我的程序中的所有内容似乎都运行正常,但是由于某些奇怪的原因,无论何时它到达输入部分,您选择乘法或除以它,然后终止程序就像它已经完成一样。
这有什么问题?
我尝试过更改if(Choice.equals(Multiply))
到" Multiply
"另一个也是如此。
但没有运气。
这是我的代码:
import java.util.Scanner;
public class APJAVAPROGRAM {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner numberinput = new Scanner(System.in);
Scanner stringinput = new Scanner(System.in);
System.out.print("Input a numerator >>>"); // inputs first Numerator
double Numerator = numberinput.nextDouble();
System.out.print("Input a Denominator >>>"); // Inputs first denominator
double Denominator = numberinput.nextDouble();
System.out.print("Input a second numerator >>>"); //Inputs second Numerator
double Numerator2 = numberinput.nextDouble();
System.out.print("Input a second Denominator >>>"); // Inputs second denominator
double Denominator2 = numberinput.nextDouble();
System.out.println("Your entered fractions are " + Numerator + "/" + Denominator + " and "+ Numerator2 + "/" + Denominator2);
// Will print out the entered fractions
System.out.println("What would you like to do with these?"); //asks user what should be done
System.out.print("Multipy, Divide, Add, or Subtract >>>");
String Choice = stringinput.nextLine();
double Multiply = Numerator * Numerator2;
double Multiply2 = Denominator * Denominator2;
if(Choice.equals("Multiply"))
{
System.out.println(Multiply + "/" + Multiply2 + " Is your answer.");
// If the input is multiply it will do as such
}
else
{
double Divide = Numerator * Denominator2;
double Divide2 = Numerator2 * Denominator;
if(Choice.equals("Divide"))
System.out.println(Divide + "/" + Divide2 + " Is your answer.");
}
}
}
答案 0 :(得分:2)
.equals(Object obj)
区分大小写,如果您使用正确运行的大写字母输入“Multiply”或“Divide”。
使用.equalsIgnoreCase(Object obj)
进行字符串到字符串比较,忽略大小写注意事项
答案 1 :(得分:0)
我刚刚运行了你的代码,这一切都没问题。这是我得到的输入/输出:
Input a numerator >>>1
Input a Denominator >>>2
Input a second numerator >>>3
Input a second Denominator >>>4
Your entered fractions are 1.0/2.0 and 3.0/4.0
What would you like to do with these?
Multipy, Divide, Add, or Subtract >>>Multiply
3.0/8.0 Is your answer.