Java 在 CMD 中 运行 时出现编程错误
Java Programming Error When Run in CMD
今天我写的这段代码有问题。当我尝试 运行 命令提示符时出现问题,它不显示我写的最后一行代码 "Congratulations, the birth month is April"
如果有人明白为什么会有所帮助!
代码:
import java.io.*;
import java.util.Scanner;
public class Lab3_5{
// Global variable to hold sales is defined
static double age, weight, birthMonth;
public static void main(String[] args){
// Method calls
getAge();
getWeight();
getMonth();
}
// This module takes in the required user input
public static void getAge(){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your guess for age: ");
double age = keyboard.nextDouble();
if (age >= 25){
System.out.println("Congratulations, the age is 25 or less.");
}
}
// This module takes in the required user input
public static void getWeight(){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your guess for weight: ");
double weight = keyboard.nextDouble();
if (weight <= 128){
System.out.println("Congratulations, the weight is 128 or less.");
}
}
// This module takes in the required user input
public static void getMonth(){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your guess for birth month: ");
String birthMonth = keyboard.next();
if (birthMonth == "April"){
System.out.println("Congratulations, the birth month is April.");
}
}
}
与命令提示符无关
问题是:
if (birthMonth == "April"){
应该是:
if ("April".equals(birthMonth)){
String
s要和equals()
比较。
birthMonth == "April"
只有当它们是同一个对象时才为真。
这并非总是如此 equals()
比较 String
的内容。
命令提示符不是这里的问题。
不能对字符串使用“==”,需要使用equals()或equalsignorecase()
public static void getMonth(){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your guess for birth month: ");
String birthMonth = keyboard.next();
if (birthMonth.equalsIgnoreCase("April")){
System.out.println("Congratulations, the birth month is April.");
}
}
}
这是因为字符串是对象。 (注意字符串是大写的,这是 classes 的好习惯!)
因此,您正在查看两个不同的字符串 classes 并且您在询问它们是否相同,它们并没有偏离轨道。因此,通过使用 equals() 或 equalsIgnoreCase,您可以告诉 class 根据它持有的字符类型将自己与另一个进行比较。
在 java 中,字符串是一个 class,因此 birthMonth
是一个对象,所以你不能这样做
if (birthMonth == "April")
因为 ==
测试引用相等性(它们是否是同一个对象)。
你必须字符串函数 .equals()
或 equalsIgnoreCase()
(忽略大小写)来测试值是否相等(它们在逻辑上是否“相等”)。
像这样
if (birthMonth.equalsIgnoreCase("April"))
今天我写的这段代码有问题。当我尝试 运行 命令提示符时出现问题,它不显示我写的最后一行代码 "Congratulations, the birth month is April" 如果有人明白为什么会有所帮助!
代码:
import java.io.*;
import java.util.Scanner;
public class Lab3_5{
// Global variable to hold sales is defined
static double age, weight, birthMonth;
public static void main(String[] args){
// Method calls
getAge();
getWeight();
getMonth();
}
// This module takes in the required user input
public static void getAge(){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your guess for age: ");
double age = keyboard.nextDouble();
if (age >= 25){
System.out.println("Congratulations, the age is 25 or less.");
}
}
// This module takes in the required user input
public static void getWeight(){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your guess for weight: ");
double weight = keyboard.nextDouble();
if (weight <= 128){
System.out.println("Congratulations, the weight is 128 or less.");
}
}
// This module takes in the required user input
public static void getMonth(){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your guess for birth month: ");
String birthMonth = keyboard.next();
if (birthMonth == "April"){
System.out.println("Congratulations, the birth month is April.");
}
}
}
与命令提示符无关
问题是:
if (birthMonth == "April"){
应该是:
if ("April".equals(birthMonth)){
String
s要和equals()
比较。
birthMonth == "April"
只有当它们是同一个对象时才为真。
这并非总是如此 equals()
比较 String
的内容。
命令提示符不是这里的问题。
不能对字符串使用“==”,需要使用equals()或equalsignorecase()
public static void getMonth(){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your guess for birth month: ");
String birthMonth = keyboard.next();
if (birthMonth.equalsIgnoreCase("April")){
System.out.println("Congratulations, the birth month is April.");
}
}
}
这是因为字符串是对象。 (注意字符串是大写的,这是 classes 的好习惯!)
因此,您正在查看两个不同的字符串 classes 并且您在询问它们是否相同,它们并没有偏离轨道。因此,通过使用 equals() 或 equalsIgnoreCase,您可以告诉 class 根据它持有的字符类型将自己与另一个进行比较。
在 java 中,字符串是一个 class,因此 birthMonth
是一个对象,所以你不能这样做
if (birthMonth == "April")
因为 ==
测试引用相等性(它们是否是同一个对象)。
你必须字符串函数 .equals()
或 equalsIgnoreCase()
(忽略大小写)来测试值是否相等(它们在逻辑上是否“相等”)。
像这样
if (birthMonth.equalsIgnoreCase("April"))