我不确定为什么它不起作用,eclipse一直告诉我" A" " B"和" C"没有被定义为变量并且一直建议定义它们(" A"" B"" C"是用户可以输入的所有选项结果)。有人能找到解决办法吗?
/*
* George Sayegh
* Febuary 17th 2016
* program for phone plan
*/
import javax.swing.JOptionPane;
public class Feb21716 {
public static void main(String[] args) {
double Package, Data, Bill;
//Get Package
String Packagetext = JOptionPane.showInputDialog("Enter your wireless service carrier plan"
+ "\n Package A: For $29.99 per month 2GB of data is provided. Additional data is $10.00 per GB."
+ "\n Package B: For $39.99 per month 4GB of data is provided. Additional data is $5.00 per GB. "
+ "\n Package C: For $49.99 per month unlimited data is provided."
+ "\n Please enter only the letter in Upper Case ex. Package A = A");
Package = Double.parseDouble(Packagetext);
//Get Amount of Data Used
String Datatext = JOptionPane.showInputDialog("Please enter the Amount of data you use in GB"
+ "\n Please note, just enter the numbers "
+ "\n ex. 750 MB = .75 ");
if(( Package == A )&&(Data <= 2)){
// when Package is A and less than 2GB
Bill = 29.99;
} else if ((Package == A)&&(Data > 2)) {
// when package is A and more than 2GB
Bill = 29.99 + 10 * Data;
} else if ((Package == B)&&(Data <= 4)) {
// when package is B and less than 4GB
Bill = 39.99;
} else if ((Package == B)&&(Data > 4)){
// when package is B and more than 4GB
Bill = 29.99 + 10 * Data;
} else if ((Package == C)&&(Data < 50)){
// when package is C and less than 50gb
Bill = 49.99;
} else if ((Package == C)&&(Data > 50)){
// when package is C and more than 50gb
Bill = JOptionPane.showMessageDialog(null, "You have entered an invalid Number");
}
JOptionPane.showMessageDialog(null, "Your bill is: $" + Bill);
}
}
}
答案 0 :(得分:0)
不是将Package声明为double,而是将其设为char变量,Data和Bill可以保持双倍。因为Package表示单个字母值(A,B,C),所以最好存储为char。双打表示数值而不是字母。
所以这就是它在代码中的样子:
public static void main(String[] args) {
double data, bill;
char packageVal;
char package_A, package_B, package_C;
//Get Package
String packagetext = JOptionPane.showInputDialog("Enter your wireless service carrier plan"
+ "\n Package A: For $29.99 per month 2GB of data is provided. Additional data is $10.00 per GB."
+ "\n Package B: For $39.99 per month 4GB of data is provided. Additional data is $5.00 per GB. "
+ "\n Package C: For $49.99 per month unlimited data is provided."
+ "\n Please enter only the letter in Upper Case ex. Package A = A");
packageVal = Packagetext.charAt(0);
//Get Amount of Data Used
String datatext = JOptionPane.showInputDialog("Please enter the Amount of data you use in GB"
+ "\n Please note, just enter the numbers "
+ "\n ex. 750 MB = .75 ");
data = Double.parseDouble(datatext);
if(( packageVal == 'A' )&&(data <= 2)){
// when Package is A and less than 2GB
bill = 29.99;
} else if ((packageVal == 'A')&&(data > 2)) {
// when package is A and more than 2GB
bill = 29.99 + 10 * Data;
} else if ((packageVal == 'B')&&(data <= 4)) {
// when package is B and less than 4GB
bill = 39.99;
} else if ((packageVal == 'B')&&(data > 4)){
// when package is B and more than 4GB
bill = 29.99 + 10 * Data;
} else if ((packageVal == 'C')&&(data < 50)){
// when package is C and less than 50gb
bill = 49.99;
} else if ((packageVal == 'C')&&(data > 50)){
// when package is C and more than 50gb
bill = JOptionPane.showMessageDialog(null, "You have entered an invalid Number");
}
JOptionPane.showMessageDialog(null, "Your bill is: $" + bill);
这应该足以解决您的问题。
答案 1 :(得分:0)
在if语句中,您要比较Package
double
与A,B
和C
未声明的变量。
JOptionPane.showInputDialog("your text here");
返回一个String。
更好的方法是执行以下操作。
String userInput = JOptionPane.showInputDialog("Insert package A,B or C");
然后在if
语句中使用equals
方法
if(userInput.equals("A")) {
// code to run if "A" is the userInput
}
作为最后一点,请尝试遵循正确的代码约定,因为它会显着提高代码对您自己和他人的可读性。
答案 2 :(得分:0)
/*
* TMKRAY
* Febuary 18th 2016
* program for phone plan corrected
*/
import java.util.*;
import java.util.Scanner;
import java.text.DecimalFormat;
public class Feb21716 {
private static DecimalFormat df2 = new DecimalFormat(".##");
public static void main(String[] args) {
double total;
System.out.println("Enter your wireless service carrier plan"
+ "\n Package A: For $29.99 per month 2GB of data is provided. Additional data is $10.00 per GB."
+ "\n Package B: For $39.99 per month 4GB of data is provided. Additional data is $5.00 per GB. "
+ "\n Package C: For $49.99 per month unlimited data is provided."
+ "\n Please enter only the letter in Upper Case ex. Package A = A");
Scanner sc = new Scanner(System.in);
String whichPackage = sc.nextLine();
System.out.println("Please enter the Amount of data you use in GB"
+ "\n Please note, just enter the numbers "
+ "\n ex. 750 MB = .75 ");
Scanner dText = new Scanner(System.in);
double packageSize = dText.nextDouble();
if(( whichPackage .equals( "A" ))&&(packageSize <= 2)){
// when Package is A and less than 2GB
total = 29.99;
System.out.println("Your Bill is: " +df2.format(total));
} else if(( whichPackage .equals( "A" ))&&(packageSize > 2)) {
// when package is A and more than 2GB
total = 29.99 + 10 * (packageSize-2);
System.out.println("Your Bill is: " + df2.format(total));
} else if(( whichPackage .equals( "B" ))&&(packageSize <= 4)) {
// when package is B and less than 4GB
total = 39.99;
System.out.println("Your Bill is: " + df2.format(total));
} else if(( whichPackage .equals( "B" ))&&(packageSize > 4)){
// when package is B and more than 4GB
total = 29.99 + 10 * (packageSize-4);
System.out.println("Your Bill is: " + df2.format(total));
} else if(( whichPackage .equals( "C" ))&&(packageSize < 50)){
// when package is C and less than 50gb
total = 49.99;
System.out.println("Your Bill is: " + df2.format(total));
} else if(( whichPackage .equals( "C" ))&&(packageSize > 50)){
// when package is C and more than 50gb
total= 0;
System.out.println("You didnt enter a valid number "+total);
}
}
}