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

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

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

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

  1. import java.util.Scanner;
  2. public class Converter{
  3. public static void calculateMicro(double number){
  4. double answer = 304796.293632 * number;
  5. System.out.print ("The answer is: " + answer );
  6. }
  7. public static void calculateMilli(double number){
  8. double answer = 304.8 * number;
  9. System.out.print ("The answer is: " + answer );
  10. }
  11. public static void calculateCenti(double number){
  12. double answer = 30.48 * number;
  13. System.out.print ("The answer is: " + answer );
  14. }
  15. public static void calculateMeter(double number){
  16. double answer = 0.3048 * number;
  17. System.out.print ("The answer is: " + answer );
  18. }
  19. public static void calculateKilo(double number){
  20. double answer = 0.0003048 * number;
  21. System.out.print ("The answer is: " + answer );
  22. }
  23. public static void main (String args[]) {
  24. Scanner sc = new Scanner(System.in);
  25. double number = sc.nextDouble();
  26. int x = 1;
  27. int option = sc.nextInt();
  28. while(x==1){
  29. System.out.println("1.micrometer 2.millimeter 3.centimeter 4.meter 5. kilometer");
  30. if(option==1){
  31. calculateMicro(number);
  32. }
  33. else if(option==2){
  34. calculateMilli(number);
  35. }
  36. else if(option==3){
  37. calculateCenti(number);
  38. }
  39. else if(option==4){
  40. calculateMeter(number);
  41. }
  42. else if(option==5){
  43. calculateKilo(number);
  44. }
  45. }
7lrncoxx

7lrncoxx1#

你被一个无限循环

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

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

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

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

  1. switch(option) {
  2. case 1:
  3. calculateMicro(number);
  4. break;
  5. case 2:
  6. calculateMilli(number);
  7. break;
  8. //cases
  9. // default:
  10. }
展开查看全部

相关问题