Ask coding questions

← Back to all posts
3
homework help

You will notice that this code allows for interactive input using the Scanner class. Run the program with at least 2 different sets of input, including negative and positive values. Print the output screen for each set of input
int age;
Scanner input = new Scanner(System.in);
System.out.print("Enter age: ");
age = input.nextInt();
while(age <= 0){
System.out.println("Must be positive. ");
System.out.print(" Enter age: ");
age = input.nextInt();
}
System.out.println("You entered " + age);

Commentshotnewtop
2

Insteady of pasting the code in, maybe supply the repl link. You also should avoid asking homework questions. It says so on the rules, and it is your job to solve.

2

hey maybe check the rules before you post it says to avoid asking homework questions just a friendly reminder.

2

Missing int var type to declare age is supposed to be an int. Should be int age = input.nextInt();

2

@SandyyyNwankwo Do you have the repl? It helps me to interact with the code.

1

@SandyyyNwankwo what will be the else statement since the while statement is less than or equal to zero.

2

@SandyyyNwankwo I'm sure else statements only work with if and else if

1

@Geocube101 look.
import java.util.Scanner;
class Age{
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int age;
System.out.print("Enter age:");
age = input.nextInt();
while (age <= 0)
{
if (age )
System.out.print("Must be postive");
else
System.out.print("Enter age:");
int age = input.nextInt();
System.out.println("You entered " + age );

}

}

}

2

@SandyyyNwankwo if the age must be positive then you could just add 1 to the age until it is not positive. If this is the case, then the if statement is not needed. If you are trying to get the user to enter a new age until it is positive, the the if statement shouldn't be needed either. You could just ask the user to input a new age and once age < 0, the while loop will break and the code will continue.

1

@Geocube101 can you write that for me i'm confused

2

@SandyyyNwankwo
while (age < 0)
{
--System.out.println("Enter your age: ")
--int age = input.nextInt()
}
//Whatever Code Comes After This
//Dashes are representative of spaces

1

@SandyyyNwankwo like this?
import java.util.Scanner;
class Age{
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int age;
System.out.print("Enter age:");
age = input.nextInt();
(age <= 0)
{
age < 1;
System.out.print("Must be postive");
else
System.out.print("Enter age:");
int age = input.nextInt();
System.out.println("You entered " + age );

}

}

}

2

@SandyyyNwankwo Is the goal to only accept positive integers?

2

@Geocube101 So you could use loops to ask continually or automatically convert to a positive int if the input is negative. The first method is stated 4 comments ago and the second method is if negative, multiply by '-1'

1

@Geocube101 i'm confused lol. this is my first time

1

@Geocube101 thank you I'm a dummy. quick question how will I do it if it was negative.

2

@SandyyyNwankwo
if (age < 0)
{
--age = age * -1
}
//Dashes Representative of Spaces

1

@Geocube101 thank you, can you help me with other problems please.

1

@SandyyyNwankwo Repl.it is not used to answer homework questions, sorry! @timmy_i_chen

2

@Geocube101 It didn't work for negatives

1

@Geocube101 what about negative I got a error

1

@SandyyyNwankwo What do you mean? I tried the link and it returns only positive ints. Can I see the error?

1

@Geocube101 i'm fine on the positive values.
I need for negative values

1

@SandyyyNwankwo So you need it to return negatives?

1

@Geocube101 it didn't work. Is it like this
//**
import java.util.Scanner;
class Age {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

--age =age*1;

while (--age > 0)
{
	--age =age*1;
	System.out.println("Must be negative. ");
  System.out.println("Enter your age:");
  age = sc.nextInt();
}
System.out.println("You entered " + age);

}
}

1

@SandyyyNwankwo The dashes were only there to represent spaces. Match the indentation of line 5 and 6 with that of line 7 and 8

1

@Geocube101 i'm confused the goal to only accept negative integers?

1

@SandyyyNwankwo In the code pasted 3 comments ago, the goal is to only pass negative ints.

1

@Geocube101 this if (age < 0)
{
--age = age * -1
?
I got an error

1

@SandyyyNwankwo Replace the dashes with spaces