如何在 java 中的 do while 循环的 "while" 部分打印出来?
How can I print out in the "while" section of a do while loop in java?
这是我当前的代码。如果用户输入“0”,我希望它 return a System.out.println("Please enter a number above zero")。它目前只是重复,如果用户输入 0
目前,它检查用户是否输入了 0 并重复。
import java.util.Scanner;
public class MyProgram {
public static void main(String[] args) {
// call scanner for user input
Scanner in = new Scanner(System.in);
// double for salary
int salary = 0;
double incomeTax = 0;//
do {
System.out.println("Please enter your salary?");
try {
salary = in.nextInt();
// test if user enters something other than an integer
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input, only enter a number above 0");
salary = Integer.MIN_VALUE;
in.next(); // consume the non-int so we don't get caught in an endless loop
}
} while (salary <= 0); // loop as long as the salary is less than zero
if (salary <= 18000 & salary > 0) {
incomeTax = 0;
System.out.println("your weekly income is $" + salary + ", you will pay no income tax this financial year.");
}
in.close();
}
}
尽管我会在问题中添加预期值。出错时,您推荐的字符串可能会打印如下。希望我正确地理解了你的问题并且这回答了它。
do {
System.out.println("Please enter your salary? (> 0)");
try {
salary = in.nextInt();
// test if user enters something other than an integer
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input, only enter a number above 0");
salary = Integer.MIN_VALUE;
in.next(); // consume the non-int so we don't get caught in an endless loop
}
if (salary <= 0) {
System.out.Println("Please enter a number above zero");
}
} while (salary <= 0); // loop as long as the salary is less than zero
这是我当前的代码。如果用户输入“0”,我希望它 return a System.out.println("Please enter a number above zero")。它目前只是重复,如果用户输入 0
目前,它检查用户是否输入了 0 并重复。
import java.util.Scanner;
public class MyProgram {
public static void main(String[] args) {
// call scanner for user input
Scanner in = new Scanner(System.in);
// double for salary
int salary = 0;
double incomeTax = 0;//
do {
System.out.println("Please enter your salary?");
try {
salary = in.nextInt();
// test if user enters something other than an integer
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input, only enter a number above 0");
salary = Integer.MIN_VALUE;
in.next(); // consume the non-int so we don't get caught in an endless loop
}
} while (salary <= 0); // loop as long as the salary is less than zero
if (salary <= 18000 & salary > 0) {
incomeTax = 0;
System.out.println("your weekly income is $" + salary + ", you will pay no income tax this financial year.");
}
in.close();
}
}
尽管我会在问题中添加预期值。出错时,您推荐的字符串可能会打印如下。希望我正确地理解了你的问题并且这回答了它。
do {
System.out.println("Please enter your salary? (> 0)");
try {
salary = in.nextInt();
// test if user enters something other than an integer
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input, only enter a number above 0");
salary = Integer.MIN_VALUE;
in.next(); // consume the non-int so we don't get caught in an endless loop
}
if (salary <= 0) {
System.out.Println("Please enter a number above zero");
}
} while (salary <= 0); // loop as long as the salary is less than zero