这是一个简单的计算器,我需要使用基本命令才能运行。这个项目的目标是编写异常(非常简单),但是,对于我的生活,我无法弄清楚这一点。我到处寻找。
无论是if / else语句还是Switch / Case语句,都会跳过thrid语句。当用户输入" m"它应该将计算的值保存到占位符变量中以便能够被调用(同样,超级简单)。我在添加部分添加了一个默认的case语句,并将我的save方法添加到默认语句中,它完美地运行。每个其他命令(r用于调用,c用于清除,e用于退出)也很有效。 m for save完全没有.....
apply plugin: 'groovy'
repositories {
maven {
url repository_url
credentials {
username = artifactory_user
password = artifactory_password
}
}
}
dependencies {
compile(
'com.oracle:ojdbc6:11.2.0.1.0',
'javax.xml.bind:jsr173_api:1.0',
'org.apache.directory:groovy-ldap:1.0',
'org.codehaus.groovy:groovy-all:2.4.5',
'weblogic:wlfullclient:10.3.6',
'javax.jms:jms:1.1'
)
testCompile(
'junit:junit:4.12',
'org.spockframework:spock-core:1.0-groovy-2.4'
)
}
sourceSets {
unit {
groovy {
srcDir file('src/test/unit/groovy')
exclude '**/integration/**'
}
resources {
srcDir file('src/test/resources')
}
compileClasspath += sourceSets.main.output + sourceSets.test.output + configurations.testRuntime
runtimeClasspath += sourceSets.main.output + sourceSets.test.output + configurations.testRuntime
}
integration {
groovy {
srcDir 'src/test/integration/groovy'
exclude '**/unit/**'
}
resources {
srcDir 'src/test/resources'
}
compileClasspath += sourceSets.main.output + sourceSets.test.output + configurations.testRuntime
runtimeClasspath += sourceSets.main.output + sourceSets.test.output + configurations.testRuntime
}
}
configurations {
unitCompile.extendsFrom testCompile
unitRuntime.extendsFrom testRuntime
integrationCompile.extendsFrom testCompile
integrationRuntime.extendsFrom testRuntime
}
task unit(type: Test) {
include '**/unit/**'
testClassesDir = sourceSets.unit.output.classesDir
classpath = sourceSets.unit.runtimeClasspath
}
task integration(type: Test) {
include '**/integration/**'
testClassesDir = sourceSets.integration.output.classesDir
classpath = sourceSets.integration.runtimeClasspath
outputs.upToDateWhen { false }
}
}
有问题的主要代码是:我知道其他人没有默认或中断声明。这个,我正在调试并试图找出m失败的原因。现在,如果你是他,它只是转到默认的case语句,但没有解决问题。
import java.util.Scanner;
public class Calculator {
public static Scanner input = new Scanner(System.in);
public static double placeHolder;
public static void clear() {
System.out.println("Save has been deleted, Screen has been cleared.");
}
public static void end() {
System.out.println("Program ended..");
input.close();
System.exit(0);
}
public static void save(double initValue) {
System.out.println("Number saved!");
placeHolder = initValue;
}
public static void recall() {
if (placeHolder != 0){
System.out.println("Memory Place Holder Set To: " + placeHolder);
}
else {
System.out.println("There is no data saved.");
}
}
public static void commands() {
System.out.println("e = end | c = clear | m = save | r = recall | o = continue");
String command = input.nextLine();
if (command.equals("e")){
end();
}
else if (command.equals("c")){
clear();
}
else if (command.equals("r")){
recall();
}
}
public static void main(String[] args) {
boolean loop = true;
while (loop == true){
commands();
System.out.println("Please enter what you would like to do: (+,-,*,/,%)");
String function = input.nextLine();
System.out.println("Enter the first number to be calucalted (If dividing, this is the numerator):");
double n1 = input.nextDouble();
System.out.println("Enter the second number to be calucalted (If dividing, this is the denominator):");
double n2 = input.nextDouble();
//=======================
// Addition
//=======================
if (function.equals("+")){
double sum = n1+n2;
System.out.println(n1+"+"+ n2 +" = " + sum);
System.out.println("e = end | c = clear | m = save | r = recall");
String command = input.nextLine();
input.nextLine();
switch (command){
case "e":
end();
break;
case "c":
clear();
break;
case "m":
save(sum);
break;
case "r":
recall();
break;
default:
System.out.println("Default");
save(sum);
break;
}
}
//=======================
// Subtraction
//=======================
else if (function.equals("-")){
double sum = n1-n2;
System.out.println(n1 + "-" + n2 +" = " + sum);
System.out.println("e = end | c = clear | m = save | r = recall");
String command = input.nextLine();
input.nextLine();
switch (command){
case "e":
end();
case "c":
clear();
case "m":
save(sum);
case "r":
recall();
}
}
//=======================
// Multiplication
//=======================
else if (function.equals("*")){
double sum = n1*n2;
System.out.println(n1 + "*" + n2 +" = " + sum);
System.out.println("e = end | c = clear | m = save | r = recall");
String command = input.nextLine();
input.nextLine();
switch (command){
case "e":
end();
case "c":
clear();
case "m":
save(sum);
case "r":
recall();
}
}
//=======================
// Division
//=======================
else if (function.equals("/")){
double sum = n1/n2;
System.out.println(n1 + "/" + n2 +" = " + sum);
System.out.println("e = end | c = clear | m = save | r = recall");
String command = input.nextLine();
input.nextLine();
switch (command){
case "e":
end();
case "c":
clear();
case "m":
save(sum);
case "r":
recall();
}
}
//=======================
// Mod
//=======================
else if (function.equals("%")){
double sum = n1%n2;
System.out.println(n1 + "%" + n2 +" = " + sum);
System.out.println("e = end | c = clear | m = save | r = recall");
String command = input.nextLine();
input.nextLine();
switch (command){
case "e":
end();
case "c":
clear();
case "m":
save(sum);
case "r":
recall();
}
}
}
//=======================
// Dictate loop duration:
//=======================
System.out.println("Would you like to continue? (Y|N): ");
String ans = input.nextLine();
if (ans.equals("N") || ans.equals("n")){
System.out.println("Closing Program");
loop = false;
end();
}
}
=============================================== ========================== 以下是在事后查看此帖子的人的修复:
switch (command){
case "e":
end();
break;
case "c":
clear();
break;
case "m":
save(sum);
break;
case "r":
recall();
break;
default:
System.out.println("Default");
save(sum);
break;
答案 0 :(得分:1)
input.nextDouble();
后,您需要致电input.nextLine();
消费新行,请参阅Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods input.nextLine();
String command = input.nextLine();
醇>
答案 1 :(得分:1)
nextDouble()
不消耗新行。您可以通过解析整行来解决这个问题,然后使用Double.parseDouble()
来获取此行中的double值
....
System.out.println("Enter the first number to be"+
"calculated (If dividing, this is the numerator):");
double n1 = Double.parseDouble(input.nextLine());
System.out.println("Enter the second number to be" +
"calculated (If dividing, this is the denominator):");
double n2 = Double.parseDouble(input.nextLine());
if (function.equals("+")) {
double sum = n1 + n2;
System.out.println(n1 + "+" + n2 + " = " + sum);
System.out.println("e = end | c = clear | m = save | r = recall");
String command = input.nextLine();
switch (command) {
case "e":
end();
break;
case "c":
clear();
break;
case "m":
save(sum);
break;
case "r":
recall();
break;
}
....
注意强>
您不必使用布尔变量检查while(loop == true)
,您可以通过while(loop)
进行检查。
答案 2 :(得分:-1)
您需要照顾的事情很少。
我对您的代码所做的更改 -
placeHolder
方法clear()
值
input.next()
代替input.nextLine()
并删除额外的input.newLine()
方法您需要在代码中模块化很多东西。看看Best Coding练习
答案 3 :(得分:-1)
input.nextDouble()
读取数字,但对行尾没有任何作用。所以当你稍后在代码中执行此操作时:
String command = input.nextLine();
它处理线的左端,而不是" m"你输入下一个。
要修复,请执行一些操作来处理输入的行尾,例如在调用nextDouble()之后添加input.nextLine():
double n1 = input.nextDouble();
input.nextLine();
double n2 = input.nextDouble();
input.nextLine();