java—它没有显示任何错误,但编译器只是继续加载或显示[object]

5us2dqdw  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(367)

**结束。**此问题需要详细的调试信息。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

21天前关门了。
改进这个问题
我想做一个长度转换器,可以把脚转换成其他单位,如微米,毫米。。。。。。当我试着运行它时,它只是一直在加载,有时会显示[object]…(我不知道[object]是不是一个错误)

import java.util.Scanner;
public class Converter{

public static void calculateMicro(double number){
double answer =   304796.293632 * number;
System.out.print ("The answer is: " + answer );
}
public static void calculateMilli(double number){
double answer =   304.8 * number;
System.out.print ("The answer is: " + answer );
}
public static void calculateCenti(double number){
double answer =   30.48 * number;
System.out.print ("The answer is: " + answer );
}
public static void calculateMeter(double number){
double answer =   0.3048 * number;
System.out.print ("The answer is: " + answer );
}
public static void calculateKilo(double number){
double answer =   0.0003048 * number;
System.out.print ("The answer is: " + answer );
}
public static void main (String args[]) {
Scanner sc = new Scanner(System.in);

double number = sc.nextDouble();
int x = 1;
int option = sc.nextInt();

while(x==1){
   System.out.println("1.micrometer  2.millimeter  3.centimeter  4.meter  5. kilometer");
  if(option==1){
    calculateMicro(number);
  }
  else if(option==2){
    calculateMilli(number);
  }
  else if(option==3){
    calculateCenti(number);
  }
  else if(option==4){
    calculateMeter(number);
  }
  else if(option==5){
    calculateKilo(number);
  }
}
7lrncoxx

7lrncoxx1#

你被一个无限循环

int x = 1;
while(x==1){
//code...
}

你应该
向用户询问选项
询问用户是否想再试一次(将x改为0或1)

while(x==1){
   System.out.println("1.micrometer  2.millimeter  3.centimeter  4.meter  5. kilometer");
x = sc.nextInt();
  //code to convert....
 System.out.println("do you wanna try again 1 for Yes or 0 for No");
x = sc.nextInt();
}

我认为如果你有多项选择,你应该试着切换,而不是如果其他。。。。

switch(option) {
  case 1:
        calculateMicro(number);
    break;
  case 2:
        calculateMilli(number);
    break;
  //cases
//  default:
}

相关问题