[英]Java code adding do while loop to make it run until user enters 0
你好,這是我的代碼,我需要幫助添加一個 do-while 循環以使其連續運行,直到用戶輸入零,有人可以幫忙嗎? zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz下面是代碼:
導入 java.util.Scanner;
公共類計算{
static Scanner in = new Scanner(System.in);
public static double sin(double a) {
double res = 0.0;
res = Math.sin(a);
return res;
}
public static double cos(double a) {
double res = 0.0;
res = Math.cos(a);
return res;
}
public static double tan(double a) {
double res = 0.0;
res = Math.tan(a);
return res;
}
public static double floor(double a) {
double res = 0.0;
res = Math.floor(a);
return res;
}
public static double ceil(double a) {
double res = 0.0;
res = Math.ceil(a);
return res;
}
public static double sqrt(double a) {
double res = 0.0;
res = Math.sqrt(a);
return res;
}
public static double cbrt(double a) {
double res = 0.0;
res = Math.cbrt(a);
return res;
}
public static double round(double val1) {
double res = 0.0;
res = Math.round(val1);
return res;
}
public static double min(double a, double b) {
double res = 0.0;
res = Math.min(a, b);
return res;
}
public static double max(double a, double b) {
double res = 0.0;
res = Math.max(a, b);
return res;
}
public static void input() {
do{
System.out.println("Welcome to my scientific calculator:");
System.out.println("Enter 0 to quit");
System.out.println("Enter 1 to calculate sin value");
System.out.println("Enter 2 to calculate cos value");
System.out.println("Enter 3 to calculate tan value");
System.out.println("Enter 4 to calculate floor value");
System.out.println("Enter 5 to calculate ceil value");
System.out.println("Enter 6 to calculate sqrt value");
System.out.println("Enter 7 to calculate cbrt value");
System.out.println("Enter 8 to calculate round value");
System.out.println("Enter 9 to calculate min value");
System.out.println("Enter 10 to calculate max value");
System.out.println("Enter 11 to multiply");
System.out.println("Enter 12 to divide");
System.out.println("Enter 13 to subtract");
System.out.println("Enter 14 to add");
int input = in.nextInt();
double val1 = 0.0, val2 = 0.0;
switch (input) {
case 0:
System.out.println("You quit!");
break;
case 1:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Sin-" + val1 + "=" + sin(val1));
break;
case 2:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Cos2-" + val1 + "=" + cos(val1));
break;
case 3:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Tan-" + val1 + "=" + tan(val1));
break;
case 4:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Floor-" + val1 + "=" + floor(val1));
break;
case 5:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Ceil-" + val1 + "=" + ceil(val1));
break;
case 6:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Sqrt-" + val1 + "=" + sqrt(val1));
break;
case 7:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Cbrt-" + val1 + "=" + cbrt(val1));
break;
case 8:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("round-" + val1 + "=" + round(val1));
break;
case 9:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
System.out.println("The smallest number of the two numbers is " + Math.min(val1,val2));
break;
case 10:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
System.out.println("The larger number of the two numbers is " + Math.max(val1,val2));
break;
case 11:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
double total =val1*val2;
System.out.println("The total is " + total);
break;
case 12:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
double total2 =val1/val2;
System.out.println("The total is " + total2);
break;
case 13:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
double total1 =val1-val2;
System.out.println("The total is " + total1);
break;
case 14:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
double total3 =val1+val2;
System.out.println("The total is " + total3);
break;
default:
System.out.println("Thank You for using my calculator!");
}while(input!=0);
}
}
}
public static void main(String[] args) {
input();
}
}
如果零的意思是“0”,您可以簡單地添加一個開關盒來打印一條消息並退出類似的東西。
case 0:
System.out.println("Thank You for using my calculator!");
System.exit()
break;
如果沒有輸入那么你可以使用 in.hasNextInt() 來終止
import java.util.Scanner;
public class calc {
static Scanner in = new Scanner(System.in);
public static double sin(double a) {
double res = 0.0;
res = Math.sin(a);
return res;
}
public static double cos(double a) {
double res = 0.0;
res = Math.cos(a);
return res;
}
public static double tan(double a) {
double res = 0.0;
res = Math.tan(a);
return res;
}
public static double floor(double a) {
double res = 0.0;
res = Math.floor(a);
return res;
}
public static double ceil(double a) {
double res = 0.0;
res = Math.ceil(a);
return res;
}
public static double sqrt(double a) {
double res = 0.0;
res = Math.sqrt(a);
return res;
}
public static double cbrt(double a) {
double res = 0.0;
res = Math.cbrt(a);
return res;
}
public static double round(double val1) {
double res = 0.0;
res = Math.round(val1);
return res;
}
public static double min(double a, double b) {
double res = 0.0;
res = Math.min(a, b);
return res;
}
public static double max(double a, double b) {
double res = 0.0;
res = Math.max(a, b);
return res;
}
public static void input() {
System.out.println("Welcome to my scientific calculator:");
int input;//added this
do {//added this
System.out.println("Enter 0 to quit");
System.out.println("Enter 1 to calculate sin value");
System.out.println("Enter 2 to calculate cos value");
System.out.println("Enter 3 to calculate tan value");
System.out.println("Enter 4 to calculate floor value");
System.out.println("Enter 5 to calculate ceil value");
System.out.println("Enter 6 to calculate sqrt value");
System.out.println("Enter 7 to calculate cbrt value");
System.out.println("Enter 8 to calculate round value");
System.out.println("Enter 9 to calculate min value");
System.out.println("Enter 10 to calculate max value");
System.out.println("Enter 11 to multiply");
System.out.println("Enter 12 to divide");
System.out.println("Enter 13 to subtract");
System.out.println("Enter 14 to add");
input = in.nextInt();//changed this
double val1 = 0.0, val2 = 0.0;
switch (input) {
case 1:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Sin-" + val1 + "=" + sin(val1));
break;
case 2:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Cos2-" + val1 + "=" + cos(val1));
break;
case 3:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Tan-" + val1 + "=" + tan(val1));
break;
case 4:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Floor-" + val1 + "=" + floor(val1));
break;
case 5:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Ceil-" + val1 + "=" + ceil(val1));
break;
case 6:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Sqrt-" + val1 + "=" + sqrt(val1));
break;
case 7:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Cbrt-" + val1 + "=" + cbrt(val1));
break;
case 8:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("round-" + val1 + "=" + round(val1));
break;
case 9:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
System.out.println("The smallest number of the two numbers is " + Math.min(val1, val2));
break;
case 10:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
System.out.println("The larger number of the two numbers is " + Math.max(val1, val2));
break;
case 11:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
double total = val1 * val2;
System.out.println("The total is " + total);
break;
case 12:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
double total2 = val1 / val2;
System.out.println("The total is " + total2);
break;
case 13:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
double total1 = val1 - val2;
System.out.println("The total is " + total1);
break;
case 14:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
double total3 = val1 + val2;
System.out.println("The total is " + total3);
break;
default:
System.out.println("Thank You for using my calculator!");
}
} while (input != 0);//added this
}
public static void main(String[] args) {
input();
}
}
你的程序有很多錯誤。 我已經改正了。
import java.util.Scanner;
public class calc {
public static double sin(double a) {
double res = 0.0;
res = Math.sin(a);
return res;
}
public static double cos(double a) {
double res = 0.0;
res = Math.cos(a);
return res;
}
public static double tan(double a) {
double res = 0.0;
res = Math.tan(a);
return res;
}
public static double floor(double a) {
double res = 0.0;
res = Math.floor(a);
return res;
}
public static double ceil(double a) {
double res = 0.0;
res = Math.ceil(a);
return res;
}
public static double sqrt(double a) {
double res = 0.0;
res = Math.sqrt(a);
return res;
}
public static double cbrt(double a) {
double res = 0.0;
res = Math.cbrt(a);
return res;
}
public static double round(double val1) {
double res = 0.0;
res = Math.round(val1);
return res;
}
public static double min(double a, double b) {
double res = 0.0;
res = Math.min(a, b);
return res;
}
public static double max(double a, double b) {
double res = 0.0;
res = Math.max(a, b);
return res;
}
public static void input() {
Scanner in = new Scanner(System.in);
do{
System.out.println("Welcome to my scientific calculator:");
System.out.println("Enter 0 to quit");
System.out.println("Enter 1 to calculate sin value");
System.out.println("Enter 2 to calculate cos value");
System.out.println("Enter 3 to calculate tan value");
System.out.println("Enter 4 to calculate floor value");
System.out.println("Enter 5 to calculate ceil value");
System.out.println("Enter 6 to calculate sqrt value");
System.out.println("Enter 7 to calculate cbrt value");
System.out.println("Enter 8 to calculate round value");
System.out.println("Enter 9 to calculate min value");
System.out.println("Enter 10 to calculate max value");
System.out.println("Enter 11 to multiply");
System.out.println("Enter 12 to divide");
System.out.println("Enter 13 to subtract");
System.out.println("Enter 14 to add");
int option = in.nextInt();
double val1 = 0.0, val2 = 0.0;
if (option==0){
System.out.println("You quit!");
break;
}
switch (option) {
case 1:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Sin-" + val1 + "=" + sin(val1));
break;
case 2:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Cos2-" + val1 + "=" + cos(val1));
break;
case 3:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Tan-" + val1 + "=" + tan(val1));
break;
case 4:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Floor-" + val1 + "=" + floor(val1));
break;
case 5:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Ceil-" + val1 + "=" + ceil(val1));
break;
case 6:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Sqrt-" + val1 + "=" + sqrt(val1));
break;
case 7:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Cbrt-" + val1 + "=" + cbrt(val1));
break;
case 8:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("round-" + val1 + "=" + round(val1));
break;
case 9:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
System.out.println("The smallest number of the two numbers is " + Math.min(val1,val2));
break;
case 10:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
System.out.println("The larger number of the two numbers is " + Math.max(val1,val2));
break;
case 11:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
double total =val1*val2;
System.out.println("The total is " + total);
break;
case 12:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
double total2 =val1/val2;
System.out.println("The total is " + total2);
break;
case 13:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
double total1 =val1-val2;
System.out.println("The total is " + total1);
break;
case 14:
System.out.println("Please enter the value :");
val1 = in.nextDouble();
System.out.println("Please enter the value :");
val2 = in.nextDouble();
double total3 =val1+val2;
System.out.println("The total is " + total3);
break;
default:
System.out.println("Thank You for using my calculator!");
}
}while(true);
}
public static void main(String[] args) {
input();
}
}
運行它並找到差異並意識到您的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.