gpt4 book ai didi

java - 计算 : How to add the operator before each number like a real calc

转载 作者:行者123 更新时间:2023-12-02 10:23:35 25 4
gpt4 key购买 nike

我只是想构建一个计算器,但我不知道如何在每个数字之前提示运算符。我正在尝试构建一个计算器并具有指定数量的数字输入,但他们可以通过按 -1 取消它,并且它仍然会返回最终值。

但是我尝试使用字符串语句,但我是编码新手,不知道从哪里开始。

    int operator;
int howmany;
int i=0;
int all = 0;
double div;

int scan;
System.out.println("Press 1 for addition, 2 for subtraction, 3 for \n
multiplication 4 for division or 5 for modules");
operator=scanner.nextInt();
if(operator < 1 || operator >5) {System.out.println("Restart
calculator");System.exit(0);}
System.out.println("How many numbers will you be using?");
howmany = scanner.nextInt() -1 ;


if (operator == 1){

System.out.println("Press -1 to cancel ");

while(i <=howmany) {

i++;
System.out.println("Enter number" + i);
System.out.println("So far your numbers is " + all);
scan = scanner.nextInt();
all += scan;

if(i-1 == howmany ) {
System.out.println("Your final answer is " + all);
}

if(scan == -1) {
System.out.println("No more inputs, final answer was " + all);
break;
}
}
}

if (operator == 2){

System.out.println("Press -1 to cancel ");

while(i <=howmany) {

i++;
System.out.println("Enter number" + i);
System.out.println("So far your numbers is " + all);
scan = scanner.nextInt();
all -= scan;

if(i-1 == howmany ) {
System.out.println("Your final answer is " + all);
}

if(scan == -1) {
System.out.println("No more inputs, final answer was " + all);
break;
}
}
}
if (operator == 3){
all = 1;
System.out.println("Press -1 to cancel ");

while(i <=howmany) {

i++;
System.out.println("Enter number" + i);
System.out.println("So far your numbers is " + all);
scan = scanner.nextInt();
all *= scan;

if(i-1 == howmany ) {
System.out.println("Your final answer is " + all);
}

if(scan == -1) {
System.out.println("No more inputs, final answer was " + all);
break;
}
}
}
if (operator == 4){

System.out.println("Press -1 to cancel ");

while(i <=howmany) {

i++;
System.out.println("Enter number" + i);
System.out.println("So far your numbers is " + all);
scan = scanner.nextInt();
all = scan;


if(i-1 == howmany ) {
System.out.println("Your final answer is " + all);
}

if(scan == -1) {
System.out.println("No more inputs, final answer was " + all);
break;
}
}
}
if (operator == 5){

System.out.println("Press -1 to cancel ");

while(i <=howmany) {

i++;
System.out.println("Enter number" + i);
System.out.println("So far your numbers is " + all);
scan = scanner.nextInt();
all %= scan;

if(i-1 == howmany ) {
System.out.println("Your final answer is " + all);
}

if(scan == -1) {
System.out.println("No more inputs, final answer was " + all);
break;
}
}
}
}
}

最佳答案

当你编码时不知道从哪里开始时,先把算法写成句子,然后再转换成代码会有所帮助。例如,对于计算器程序,我将从以下基本结构开始:

// need a data structure to hold all numbers
// need a data structure to hold all operations

while(the user wishes to continue) {
while(the user has not hit =) {
// use the scanner to get the next number (if it's -1, exit program)
// use the scanner to get the next operator (if it's = then exit inner
// loop to report result)
}
// need variable to hold calculation result, instantiate to first number
for(loop through numbers data structure) {
// apply next operator to the running result and next number
}
// report result to user
}

其他需要考虑的事情:计算器应该能够在计算中使用 -1。我不确定这是否是您正在从事的工作的要求,所以我将其保留为您所描述的那样,但是更符合计算器精神的做法是在每次之后询问用户如果他们想继续,则计算成功(y/n)。

关于java - 计算 : How to add the operator before each number like a real calc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54139928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com