程序未通过if语句(Java)
所以,这看起来可能很混乱,但我几个小时前才开始使用Java,我正在构建一个我脑子里已经有一段时间的程序。目前,它在最后一节挂断,询问视频中是否还有其他内容。知道为什么会这样吗?从底部以变量yn开始
import java.util.Scanner;
public class Tagger {
//inputs should include the type of video, who's in it, and what the game is called.
//Later versions can alter this, including alternate game titles.
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String mod = null;
String who2 = null;
String group = "SmeggCo";
System.out.println("Welcome to the Actagger! Let's get started.");
System.out.println("To start, is this a mod spotlight or a Let's Play?");
String type = input.nextLine();
System.out.println("Awesome! A " + type + ", eh? Sounds fun!");
try {
Thread.sleep(3000);
}
catch (InterruptedException ex){
}
System.out.println("What game is it?");
String game = input.nextLine();
try {
Thread.sleep(3000);
}
catch (InterruptedException ex){
}
System.out.println( game + "? I'll have to check that out!");
if (type == "mod spotlight"){
System.out.println("What was the mod's name?");
mod = input.next();
System.out.println("Huh, I've never heard of" + mod + ".");
}
System.out.println("Alright, last question. Who was in this video?");
String who = input.next();
System.out.println(who + " is pretty awesome!");
try {
Thread.sleep(3000);
}
catch (InterruptedException ex){
}
System.out.println("Was anyone else in the vid?");
String yn = input.next();
if (yn == "Yes"){
System.out.println("Who?");
who2 = input.next();
System.out.println("So " + who + " and" + who2 + "? Those two are weirdos!");
String[] tags = new String[5]; //Setup final array of tags. Will add to this later in 2.0
tags[0] = type;
tags[1]=game;
tags[2]=who;
tags[3]=who2;
tags[4]=mod;
tags[5]=group;
System.out.println("Here's your tags!");
System.out.println(tags);
System.out.println("Does that look right? Good!");
}
if (yn == "No"){
String[] tags = new String[5]; //Setup final array of tags. Will add to this later in 2.0
tags[0] = type;
tags[1]=game;
tags[2]=who;
tags[3]=who2;
tags[4]=mod;
tags[5]=group;
System.out.println("Here's your tags!");
System.out.println(tags);
System.out.println("Does that look right? Good!");
}
}
}
# 1 楼答案
你的平等测试是错误的。您应该使用
equals()
方法,而不是==