所以我的代碼告訴我,在if語句中,我需要插入!= null檢查。爲什麼它告訴我這個以及如何爲String變量創建if語句?我是一名初學者,非常感謝您的幫助,但請留下解釋與您的答案,因爲我並不總是知道我在做什麼,沒有具體的細節。非常感謝!如何在Java中爲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
}
}