Java "Loop" 程序?
Java "Loop" Program?
自从我完成一些 Java 编码以来已经有一段时间了,但这是一个简单的程序,我计算了我的零售店的加价。在我自己关闭程序之前,我如何才能使程序循环?现在,我 运行 这个程序在我的 terminal/command 提示符下,每次我输入一个值它都会结束,所以我不得不再次执行它,这需要一些时间(大约 0.05 秒或更短)但是我需要所有的时间来完成我正在做的这项任务。所以重点是保留它 运行ning 直到我自己关闭它。
import static java.lang.System.out;
import java.util.Scanner;
public class ProductMarkup {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner myScanner = new Scanner(System.in);
out.print("What is the quantity?");
int quantity = myScanner.nextInt();
out.print("What is the wholesale price?");
double wsprice = myScanner.nextDouble();
if (quantity == 1) {
double math = (int) (wsprice * .70);
double retail = math + wsprice;
out.println(retail + 5);
} else if (quantity == 2) {
double math = wsprice * 2;
double retail = math + 10;
out.println(retail);
} else if (quantity == 3) {
double math = wsprice * 2.5;
double retail = math + 5.75;
out.println(retail);
} else if (quantity == 4) {
double math = wsprice * 2.80;
double retail = math + 3.75;
out.println(retail);
}
}
}
正如 Phylogenesis 所说,您实际上可以将代码包装在 while(true)
中。但是,如果您想询问是否应该重新启动程序,请使用
Scanner myScanner = new Scanner(System.in);
boolean running = true;
while(running)
{
// your program code here
System.out.print("restart? ");
running = myScanner.nextLine().trim().equalsIgnoreCase("yes");
}
使用带有标记值的 while 循环来持续获取输入。对于此示例,完成后输入 -1 退出。
public class ProductMarkup {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner myScanner = new Scanner(System.in);
out.print("What is the quantity?");
int quantity = myScanner.nextInt();
out.print("What is the wholesale price?");
double wsprice = myScanner.nextDouble();
//while loop here
while(quantity != -1){
if (quantity == 1) {
double math = (int) (wsprice * .70);
double retail = math + wsprice;
out.println(retail + 5);
} else if (quantity == 2) {
double math = wsprice * 2;
double retail = math + 10;
out.println(retail);
} else if (quantity == 3) {
double math = wsprice * 2.5;
double retail = math + 5.75;
out.println(retail);
} else if (quantity == 4) {
double math = wsprice * 2.80;
double retail = math + 3.75;
out.println(retail);
out.print("What is the quantity?");
int quantity = myScanner.nextInt();
out.print("What is the wholesale price?");
double wsprice = myScanner.nextDouble();
}
}
}
在Java中,您可以使用以下三种循环之一:
- while 循环
while 循环是一种控制结构,允许您将任务重复一定次数。
语法:
while 循环的语法是:
while(Boolean_expression)
{
//Statements
}
When executing, if the boolean_expression result is true, then the actions inside the loop will be executed. This will continue as long
as the expression result is true.
- 做...while 循环
语法:
do...while 循环的语法是:
do
{
//Statements
}while(Boolean_expression);
Notice that the Boolean expression appears at the end of the loop, so
the statements in the loop execute once before the Boolean is tested.
If the Boolean expression is true, the flow of control jumps back up to do, and the statements in the loop execute again. This process
repeats until the Boolean expression is false.
- for 循环
A for loop is a repetition control structure that allows you to
efficiently write a loop that needs to execute a specific number of
times.
A for loop is useful when you know how many times a task is to be
repeated.
语法:
for循环的语法是:
for(initialization; Boolean_expression; update)
{
//Statements
}
在for循环的情况下,如果你想永远重复你的过程,你可以使用如下方法。
for(;;){
whatever process you have
}
希望我提供的内容能解决您的问题。
import static java.lang.System.out;
import java.util.Scanner;
public class ProductMarkup {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner myScanner = new Scanner(System.in);
int quantity;
do {
out.print("What is the quantity?");
quantity = myScanner.nextInt();
out.print("What is the wholesale price?");
double wsprice = myScanner.nextDouble();
if (quantity == 1) {
double math = (int) (wsprice * .70);
double retail = math + wsprice;
out.println(retail + 5);
} else if (quantity == 2) {
double math = wsprice * 2;
double retail = math + 10;
out.println(retail);
} else if (quantity == 3) {
double math = wsprice * 2.5;
double retail = math + 5.75;
out.println(retail);
} else if (quantity == 4) {
double math = wsprice * 2.80;
double retail = math + 3.75;
out.println(retail);
}
} while (quantity != -1);
}
}
自从我完成一些 Java 编码以来已经有一段时间了,但这是一个简单的程序,我计算了我的零售店的加价。在我自己关闭程序之前,我如何才能使程序循环?现在,我 运行 这个程序在我的 terminal/command 提示符下,每次我输入一个值它都会结束,所以我不得不再次执行它,这需要一些时间(大约 0.05 秒或更短)但是我需要所有的时间来完成我正在做的这项任务。所以重点是保留它 运行ning 直到我自己关闭它。
import static java.lang.System.out;
import java.util.Scanner;
public class ProductMarkup {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner myScanner = new Scanner(System.in);
out.print("What is the quantity?");
int quantity = myScanner.nextInt();
out.print("What is the wholesale price?");
double wsprice = myScanner.nextDouble();
if (quantity == 1) {
double math = (int) (wsprice * .70);
double retail = math + wsprice;
out.println(retail + 5);
} else if (quantity == 2) {
double math = wsprice * 2;
double retail = math + 10;
out.println(retail);
} else if (quantity == 3) {
double math = wsprice * 2.5;
double retail = math + 5.75;
out.println(retail);
} else if (quantity == 4) {
double math = wsprice * 2.80;
double retail = math + 3.75;
out.println(retail);
}
}
}
正如 Phylogenesis 所说,您实际上可以将代码包装在 while(true)
中。但是,如果您想询问是否应该重新启动程序,请使用
Scanner myScanner = new Scanner(System.in);
boolean running = true;
while(running)
{
// your program code here
System.out.print("restart? ");
running = myScanner.nextLine().trim().equalsIgnoreCase("yes");
}
使用带有标记值的 while 循环来持续获取输入。对于此示例,完成后输入 -1 退出。
public class ProductMarkup {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner myScanner = new Scanner(System.in);
out.print("What is the quantity?");
int quantity = myScanner.nextInt();
out.print("What is the wholesale price?");
double wsprice = myScanner.nextDouble();
//while loop here
while(quantity != -1){
if (quantity == 1) {
double math = (int) (wsprice * .70);
double retail = math + wsprice;
out.println(retail + 5);
} else if (quantity == 2) {
double math = wsprice * 2;
double retail = math + 10;
out.println(retail);
} else if (quantity == 3) {
double math = wsprice * 2.5;
double retail = math + 5.75;
out.println(retail);
} else if (quantity == 4) {
double math = wsprice * 2.80;
double retail = math + 3.75;
out.println(retail);
out.print("What is the quantity?");
int quantity = myScanner.nextInt();
out.print("What is the wholesale price?");
double wsprice = myScanner.nextDouble();
}
}
}
在Java中,您可以使用以下三种循环之一:
- while 循环
while 循环是一种控制结构,允许您将任务重复一定次数。
语法: while 循环的语法是:
while(Boolean_expression)
{
//Statements
}
When executing, if the boolean_expression result is true, then the actions inside the loop will be executed. This will continue as long as the expression result is true.
- 做...while 循环
语法: do...while 循环的语法是:
do
{
//Statements
}while(Boolean_expression);
Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested.
If the Boolean expression is true, the flow of control jumps back up to do, and the statements in the loop execute again. This process repeats until the Boolean expression is false.
- for 循环
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
A for loop is useful when you know how many times a task is to be repeated.
语法: for循环的语法是:
for(initialization; Boolean_expression; update)
{
//Statements
}
在for循环的情况下,如果你想永远重复你的过程,你可以使用如下方法。
for(;;){
whatever process you have
}
希望我提供的内容能解决您的问题。
import static java.lang.System.out;
import java.util.Scanner;
public class ProductMarkup {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner myScanner = new Scanner(System.in);
int quantity;
do {
out.print("What is the quantity?");
quantity = myScanner.nextInt();
out.print("What is the wholesale price?");
double wsprice = myScanner.nextDouble();
if (quantity == 1) {
double math = (int) (wsprice * .70);
double retail = math + wsprice;
out.println(retail + 5);
} else if (quantity == 2) {
double math = wsprice * 2;
double retail = math + 10;
out.println(retail);
} else if (quantity == 3) {
double math = wsprice * 2.5;
double retail = math + 5.75;
out.println(retail);
} else if (quantity == 4) {
double math = wsprice * 2.80;
double retail = math + 3.75;
out.println(retail);
}
} while (quantity != -1);
}
}