我需要一些帮助来解决如何处理一个java程序,该程序允许用户分别输入3个不同的字母,并告诉他们是否在一个设置的单词中,如果是,则在什么索引处。这是介绍,所以它应该是你专家的小菜一碟。我们没有关于这一部分的说明,所以我真的很难过。我们所知道的只是基本字符串。到目前为止,我有。
import java.util.Scanner;
public class guessletter {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name = "carson";
int namelength = name.length();
System.out.println("The name is "+namelength+" charecters long");
System.out.println("Please enter a letter to guess?");
String letter1 = scan.nextLine();
System.out.println("Please enter a letter to guess?");
String letter2 = scan.nextLine();
System.out.println("Please enter a letter to guess?");
String letter3 = scan.nextLine();
}
}
更新:忘记提及用户必须能够在最后猜出这个词以确定它们是否正确。我想我已经把它弄下来,但它说正确的名字是错误的。
import java.util.Scanner;
public class guessletter
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String name = "carson";
String name2;
int namelength = name.length();
System.out.println("The name is "+namelength+" charecters long");
System.out.println("Please enter a letter to guess?");
char letter = scan.nextLine().charAt(0); // Gets the first character in the input
checkLetter(name, letter);
System.out.println("Please enter a letter to guess?");
letter = scan.nextLine().charAt(0);
checkLetter(name, letter);
System.out.println("Please enter a letter to guess?");
letter = scan.nextLine().charAt(0);
checkLetter(name, letter);
System.out.println("Please guess what the name is.");
name2= scan.nextLine();
if (name2 == name)
System.out.println("Yes that is the name");
else
System.out.println("Nope, that is not the name");
System.out.println("The name is "+name+"");
}
public static void checkLetter(String name, char letter)
{
int indexOfLetter = name.indexOf(letter);
if (indexOfLetter == -1)
System.out.println("That letter isn't found in the name");
else
System.out.println("That Letter is in name. It is located at index " + (indexOfLetter+1));
}
}
答案 0 :(得分:2)
注意用户不一定只输入一个字母。您应该考虑这一点并仅测试输入字符串的第一个字符。
一种可能的方法如下:
在此代码中,您只使用.charAt(0)
扫描每个输入字符串中的第一个字符(根据需要),然后使用.indexOf(letter)
上的name
方法测试它是否在字符串中根据需要变量。
import java.util.Scanner;
public class guessletter
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String name = "carson";
int namelength = name.length();
System.out.println("The name is "+namelength+" charecters long");
System.out.println("Please enter a letter to guess?");
char letter = scan.nextLine().charAt(0); // Gets the first character in the input
checkLetter(name, letter);
System.out.println("Please enter a letter to guess?");
letter = scan.nextLine().charAt(0);
checkLetter(name, letter);
System.out.println("Please enter a letter to guess?");
letter = scan.nextLine().charAt(0);
checkLetter(name, letter);
}
public static void checkLetter(String name, char letter)
{
int indexOfLetter = name.indexOf(letter);
if (indexOfLetter == -1)
System.out.prinln("Letter is not in name");
else
System.out.println("Letter is in name! index is " + (indexOfLetter+1));
}
}
答案 1 :(得分:1)
使用方法 String #indexOf ,如果给定的char不在单词中,则方法返回-1。
String name = "carson";
System.out.println(name.indexOf("a"));
System.out.println(name.indexOf("z"));
答案 2 :(得分:0)
这将为您完成工作,
import java.util.Scanner;
public class guess
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String name = "carson";
int namelength = name.length();
int l=3;
String letter;
System.out.println("The name is "+namelength+" charecters long");
while(l-->0){
System.out.println("Please enter a letter to guess?");
letter= scan.nextLine();
if(name.indexOf(letter)!=-1){
System.out.println("Letter found at position "+(name.indexOf(letter)+1));
}else{
System.out.println("Letter not found");
}
}
}
}
答案 3 :(得分:0)
您也可以使用.contains for Strings
if(name.contains("a")){
//code
}