当前位置: 首页 > 知识库问答 >
问题:

如何让计算器询问用户是否要在Java中执行另一个计算?

单于海荣
2023-03-14

我尝试使用do-while循环来实现这一点,但是找不到变量。我的目标是基本上在用户需要时重新执行代码。有人能帮忙吗?

import java.util.Scanner;
public class Calculator {
    public static void main(String args[]) {
        do{
            Scanner typed=new Scanner(System.in);
            System.out.println("Please type: 1 for add, 2 for subtract, 3 for multiply or 4 for divide");
            int userInput=typed.nextInt();
            System.out.println("Please enter the first number to calculate:");
            double number1=typed.nextDouble();
            System.out.println("Please enter the second number to calculate:");
            double number2=typed.nextDouble();
            if (userInput==1){
                System.out.println("The answer is");
                double result=number1+number2;
                System.out.println(result);
            }
            if (userInput==2){
                System.out.println("The answer is");
                double result=number1-number2;
                System.out.println(result);
            }
            if (userInput==3){
                System.out.println("The answer is");
                double result=number1*number2;
                System.out.println(result);
            }
            if (userInput==4){
                System.out.println("The answer is");
                double result=number1/number2;
                System.out.println(result);
            }
                System.out.println("Do you want to perform another calculation? Press 1 for yes or 2 for no.");
                int pressed=typed.nextInt();
            
        }while(pressed==1);
            
    }
}

共有3个答案

干永丰
2023-03-14

只需在do time循环之外声明“按下”变量。在“do{”行之前写入“int按下”,并从“int按下=typed.nextInt();”此行中删除int(即按下=typed.nextInt();)

阳光辉
2023-03-14

按可在循环体后离开作用域。将按下的声明移动到执行之前。

int pressed = 0;
do {
    // ...
    pressed=typed.nextInt();
} while(pressed==1);

或者,您可以将循环更改为无限循环,while(true) 和使用

int pressed = typed.nextInt();
if (pressed != 1) {
    break; // End the infinite loop
}
金承嗣
2023-03-14

您试图使用超出其范围的变量

    do{
      // ...
      int pressed=typed.nextInt();
        
    }while(pressed==1);  // Outside of the scope of 'pressed'

可以通过将声明移到循环外,但保留赋值来解决此问题。

    int pressed = 1;

    do{
      // ...
      pressed=typed.nextInt();
        
    }while(pressed==1);  // Outside of the scope of 'pressed'
 类似资料:
  • 问题内容: 我在Sqlite中有一个查询,其中涉及复杂的列计算,可以这样说: 我想将此计算选择为,但我还需要将其用作另一种计算的组成部分: 不幸的是,这会产生错误: 我知道我可以简单地重复计算: 但是,假设操作复杂且昂贵,是否有什么方法可以在以后重新引用它而不必重新计算呢? 问题答案: 您需要使用子查询。 结果

  • 问题内容: 我目前有一个表,用于存储大量项目的统计数据,例如视图,下载,购买等。为了对每个项目进行一次操作计数,我可以使用以下查询: 这给了我所有的内容以及他们的看法。然后,我可以将其他变量的“视图”更改为“购买”或“下载”。但是,这意味着对数据库的三个单独的调用。 一口气拿下所有三个都可以吗? 问题答案: 将返回一个表, 其中每个item_id和operation一行一行 ,包含三列:item_

  • 问题内容: 我想测试某个代码段执行的SQL查询越少越好。 似乎有其自己的方法,它将做到这一点。但是由于我没有修补ActiveRecord,所以对我来说没什么用。 RSpec或ActiveRecord是否提供任何官方的公开方式来计算在代码块中执行的SQL查询的数量? 问题答案: 我认为您通过提及回答了您自己的问题,但是这里有: 我建议您看一下后面的代码,并使用它来构建自己的方法,该方法可用于计算查询

  • 问题内容: 假设我具有以下表格结构: 是否可以运行单个查询以结合以下两个条件: 此人正在关注多少用户 从t1 WHERE userID_follower =“。$ myID”中选择COUNT(id)。。” 有多少用户关注此人 从t1 WHERE userID_following =“。$ myID”中选择COUNT(id)。 谢谢。 问题答案: 在MySql中,您可以在条件上使用该函数,因为错误条

  • 问题内容: 有没有一种方法可以查询数据库以找出所有表中有多少行? IE 希望你能指教 问题答案:

  • 我创建了一个应用程序,在我的计算机中,这个jar运行没有任何问题。我试着在另一台电脑上运行它,它什么都做不到。然后,我在另一台pc上尝试了同样的jar(如果你想这样看的话,是第三台),在那里,jar运行没有问题。所以我回到第二个,试着从命令行运行它,它给了我这个错误: “main”java.lang.UnsatisfiedLinkError头出现异常:无法加载库:C:\users\hectlr\l