【发布时间】:2015-04-15 00:08:17
【问题描述】:
所以我的代码在 if 语句中告诉我,我需要插入 != null 检查。为什么它会告诉我这个以及如何为 String 变量创建 if 语句?我是初学者,感谢您的帮助,但请在您的回答中留下解释,因为如果没有具体细节,我并不总是确切地知道自己在做什么。非常感谢!
import java.util.Scanner;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game2 extends JPanel {
public void paint(Graphics g) { //creates a void in order to paint
Graphics2D g2d = (Graphics2D) g; //creates 2D graphics
g2d.setColor(Color.YELLOW); //sets color
g2d.fillOval(350, 15, 25, 70); //draws oval
g2d.fillOval(400, 15, 25, 70); //draws
g2d.setColor(Color.RED); //sets color
g2d.fillRect(350, 120, 77, 25); //draws rectangle
Scanner game = new Scanner (System.in); //creating a new scanner
String mood, Happy, Satisfied, Sad, Nervous; //creates string
System.out.println("How are you feeling today? Pick one of the following:");
System.out.println("");
System.out.println("Happy");
System.out.println("Satisfied");
System.out.println("Sad");
System.out.println("Nervous");
mood = game.nextLine();
if (mood = Satisfied) { //I NEED HELP WITH THIS. IT TELLS ME I NEED TO "Insert '!= null' check". WHY? THANKS IN ADVANCE!
g2d.setColor(Color.YELLOW); //sets color
g2d.fillOval(350, 15, 25, 70); //draws oval
g2d.fillOval(400, 15, 25, 70); //draws oval
g2d.setColor(Color.RED); //sets color
g2d.fillRect(350, 120, 77, 25); //draws rectangle
}
}
public static void main(String[] args) { //sets this class as main
JFrame frame = new JFrame("Ping Boom"); //sets title
frame.add(new Game2());
frame.setSize(800, 500); //sets size of window
frame.setVisible(true); //sets visibility as a boolean
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //sets the ability for the window to close on command
}
}
【问题讨论】:
标签: java string if-statement