This question already has answers here:
Java help: using loops to print the squares of values in between start arg and end arg
(3 answers)
Closed 5 years ago.
I want to write a program that finds the square of all numbers up to a user inputed number. For example, if the user inputs 5, then the program outputs 1,4,9,16,25.
This is what I've tried.
import java.util.Scanner;
public class PerfectSquares {
public void numberInput(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int choice = sc.nextInt();
for(int i =0;i<choice;i++){
choice=choice*choice;
}
System.out.println("The squares are" + choice);
}
public static void main(String[] args) {
PerfectSquares input = new PerfectSquares();
input.numberInput();
}
}
The output gives me only one number which is not what I am looking for.I am new to JAVA and programming but my understanding is that you should try to create methods outside the main and then create objects to call the methods inside the main. So I am trying to create a method called numberInput and then in the main I try to call it. Please correct me if this is the wrong approach.
You are very close, the issue for your outcome is that the loop is updating the same number.
Import java.util.Scanner;
public class PerfectSquares {
public void numberInput(int max){
List<Integer> squares = new ArrayList<>();
for(int i =0;i<max;i++) {
squares.add(i^2);
}
System.out.println("The squares are: " + String.join(squares, ", ");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int choice = sc.nextInt();
PerfectSquares input = new PerfectSquares();
input.numberInput(choice);
}
Related
Non-CS major here taking my first programming class for fun. As the title states, my first assignment is "Write a program that prints out every line of input exactly as they were entered." My program right now will accept one input, but not any others. How can I fix this? Thank you so much :)
import java.util.Scanner;
public class echohw {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String ans;
ans = in.nextLine();
System.out.print(ans);
Like Scary Wombat said, use a while loop:
import java.util.Scanner;
public class echohw {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String ans;
// Continue printing user input unless "stop" is entered
while (!(ans = in.nextLine()).equals("stop"))
System.out.println(ans);
}
}
You can use array function to store multiple user inputs. In this example, I try to make it simple so you can understand it well. If you have any other doubt, you can ask me here.
import java.util.Scanner;
public class echohw {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
//Decide number of inputs
System.out.println("How many inputs you want to enter: ");
int numInput = Integer.parseInt(scan.nextLine());
//Store inputs
String aryInput[] = new String[numInput];
for (int i = 0; i < aryInput.length; i++) {
System.out.println("Enter the input " + (i+1) + " : ");
aryInput[i] = scan.nextLine();
}
//Print inputs
for (int i = 0; i < aryInput.length; i++) {
System.out.println("Input " + (i+1) + " : ");
System.out.println(aryInput[i] + "\n");
}
}
}
This question already has answers here:
Need To Take Input To Array Until User Enters 0 JAVA
(3 answers)
Closed 5 years ago.
This is the only program that i can't create. Please help me with this.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i = 0, j=0;
do {
System.out.print("Enter number: ");
i = input.nextInt();
i=j;
}
while (i == j);
System.out.println("You have inputted the same number on the previous.");
}
Cheers m8, good luck at your test :D
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i = 0, j = 999999, input = 0;
do {
i = j;
System.out.print("Enter number: ");
input = input.nextInt();
j=input;
} while (i != j);
System.out.println("You have inputted the same number on the previous.");
}
I'm totally new to Java (4 days old), and I'm trying to create my first program after watching a few YouTube videos.
Basically I'm trying to make a (guess my number game). I've created a function/method to get a random number and another function/method to get a user inputted number (both from another class called random)
I've then called these 2 values in my main method/function to be compared in a (if) statement but every time I run the program I get the same output.
Output:
Welcome to The Guessing Game
*******Version 1.1**********
Enter your name please :
john
Nice to meet you john
Ok then....let's go over the rules
I'm gonna pick a number between 1 and 10
You have 4 atempts to guess otherwise i win
Good luck!!!!
Ok i've chosen the number between 1 and 10
take a guess :
2
You are correct!!!!
I seem to get the same output every time ;-(
Sorry in advance for asking a maybe straight forward question.
(Do remember I'm a newbie and many thanks for your help.)
shaz
Below is a copy of my code:
public class GuessMain {
public static void main(String[] args) {
introduction intro = new introduction();
intro.welcome();
introduction enterName = new introduction();
enterName.userName();
introduction rules = new introduction();
rules.explainRules();
// introduction getN = introduction();
// getN.getName();
introduction glMessage = new introduction();
glMessage.goodluckMessage();
random pickRandNumber = new random();
pickRandNumber.pickRandom();
random readyMessage = new random();
readyMessage.readysteadyGo();
random guessNumobj = new random();
guessNumobj.getGuessnum();
random getNumobj = new random();
getNumobj.getNumber();
}
if (guessNumobj.getGuessnum() == getNumobj.getNumber()){
System.out.println("You are correct!!!!");
}else if (guessNumobj.getGuessnum() > getNumobj.getNumber()){
System.out.println("Too high!!!!");
}else if (guessNumobj.getGuessnum() < getNumobj.getNumber()){
System.out.println("Too low!!!!");
}
}
}
import java.util.Scanner;
public class introduction {
private String name;
public void welcome() {
System.out.println("Welcome to The Guessing Game");
System.out.println("*******Version 1.1**********");
}
public void userName() {
System.out.println("Enter your name please :");
Scanner userInput = new Scanner(System.in);
name = userInput.nextLine();
System.out.println("Nice to meet you " + name);
}
public void explainRules() {
System.out.println("Ok then....let's go over the rules");
System.out.println("I'm gonna pick a number between 1 and 10");
System.out.println("You have 4 atempts to guess otherwise i win");
}
public String getName() {
return this.name;
}
public void goodluckMessage() {
System.out.println("Good luck!!!! ");
}
}
import java.util.Random;
import java.util.Scanner;
public class random {
private int number;
private int guessNum;
public void pickRandom () {
Random getRandom = new Random();
for (int counter = 1; counter <= 1; counter++) {
number = getRandom.nextInt(10); //this stores the random number[(10){1 to 10}] in (number;) vairiable
}
}
public void readysteadyGo(){
System.out.println("Ok i've chosen the number between 1 and 10");
System.out.println("take a guess :");
Scanner scanOb = new Scanner(System.in);
guessNum = scanOb.nextInt();
}
public int getNumber(){
return this.number;
}
public int getGuessnum(){
return this.guessNum;
}
}
random is a class, and you can have any number of instances (objects) of that class. Each instance contains its own versions of number and guessNum. If you create two new random() objects, object1 and object2, and you do something that assigns to object1.number, and then you look at the value of object2.number, it will not be the value that you assigned to object1.number.
That's the problem with your code. You create one object pickRandNumber, and then call a method that sets pickRandNumber.number. You create another object readyMessage and then call a method that asks for user input and then sets readyMessage.guessNum. Then you create two new objects, and try to get the number and guessNum from the new objects. Those new objects have their own number and guessNum values, which you haven't set to anything--you've set the values of number and guessNum in different objects.
The solution is to rewrite main() to use the right objects. So after
random pickRandNumber = new random();
pickRandNumber.pickRandom();
that will set pickRandNumber.number, and if you want to retrieve that number, use something like:
if (pickRandNumber.getNumber() == ....)
I'm trying to make a simple calculator and need help with somthing
import java.util.*;
import javax.swing.*;
public class HiWorld {
public static void main(String[] args) {
System.out.println("Type 2 Numbers");
Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
answer = input1 + input2;
System.out.println(answer);
}
It says that the operator + is undefined for the argument types java.util.scanner.
What you are doing there is creating two objects that read from standard input (your keyboard) and then, you are trying to add those objects together.
For start, use:
import java.util.*;
public class HiWorld {
public static void main(String[] args) {
System.out.println("Type 2 Numbers");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
int y = input.nextInt();
int answer = x + y;
System.out.println(answer);
}
}
In Java you can only use the + operator for adding two numbers or concatenating two Strings. Not for doing anything with scanners.
Need some help trying to set up this assignment. I am not to good with arrays, nor setting up methods to be used in the main. I need to make an array of 10 random numbers 1-100, that can be compared to the user input. I only need the comparison true/false to display. Here is what I have.
I get several errors in trying to print, so i haven't even tried to compare it to the user input yet.
Thanks,
import java.util.*;
public class Final {
public static void main(String[] args) {
System.out.print("Enter Player's Free Throw Percentage: ");
Scanner input = new Scanner(System.in);
int percent = input.nextInt();
print(shots);
}
public int [] getRandomNumbers(){
int [] shots = new int [10];
Random r = new Random();
for(int i = 0; i < 10; i++)
shots[i] = r.nextInt(100);
return shots;
}
public static void print(int shots[]) {
for (int i=0; i<shots.length; i++) {
System.out.print(shots[i]);
if (i < shots.length-1) {
System.out.print(", ");
}
else {
System.out.println("");
}
}
}
}
As commentors said, please provide error messages.
However, I can pick out several issues just for starters. Let's look at your main method...
public static void main(String[] args) {
System.out.print("Enter Player's Free Throw Percentage: ");
Scanner input = new Scanner(System.in);
int percent = input.nextInt();
print int [](shots);
}
In this call to the print method, why do you have int [] in there? Are you trying to cast something to an int array? Anyways, that has to come out.
Also, you are passing the print method some shots variable that doesn't exist.
Your print method takes an int array as its only argument, so you have to pass it a valid int array. Perhaps you meant to call getRandomNumbers() and pass the int array that it returns to the print method?
Also, what's with the nested classes you're showing. You have this class Final with another class ShotClass defined inside of it. And your closing brackets are all out of whack.
In short, you need to do as the comments ask and format your code and then work through each individual error message, because you've got a whole lot to fix.
EDIT
I'm not sure if I'm doing you more harm than good by giving you the answer to your homework assignment, but I feel for you so here it is. Please just look very carefully at the exact differences between what you posted in your question and what I show below. There's several mistakes you made, including bad syntax and a misunderstanding of how scope works, and I can't properly explain all the problems without typing several pages, so I hope you can learn from this example instead...
import java.util.*;
public class Final {
public static void main(String[] args) {
System.out.print("Enter Player's Free Throw Percentage: ");
Scanner input = new Scanner(System.in);
int percent = input.nextInt();
int[] shots = getRandomNumbers();
print(shots);
}
public static int[] getRandomNumbers(){
int [] shots = new int [10];
Random r = new Random();
for(int i = 0; i < 10; i++) {
shots[i] = r.nextInt(100);
}
return shots;
}
public static void print(int[] shots) {
for (int i=0; i<shots.length; i++) {
System.out.print(shots[i]);
if (i < shots.length-1) {
System.out.print(", ");
}
else {
System.out.println("");
}
}
}
}//END class