Java代码添加do-while循环,使其运行到用户输入0为止



你好,这是我的代码,我需要帮助添加一个do while循环,使其连续运行,直到用户输入零。有人能帮忙吗?zzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzz这是代码:

导入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"是指"0",您可以简单地添加一个开关案例,打印一条消息并退出类似的操作。

case 0:
System.out.println("Thank You for using my calculator!");
System.exit()
break;

如果没有输入,则可以在.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();
}
}

试试这个,找出不同之处,并意识到你的错误。

相关内容

最新更新