我正在为我的练习制作一个刽子手程序,我正在停止如何用部分答案打印用户输入的正确字符。每件事情都是最佳的,只需要串联帮助。??????? ???是映射我需要逻辑的地方。
import java.io.*;
import java.util.Scanner;
class hangman{
public static void main(String args[]){
int counter=6;
String m="ashish";
char mj[] = m.toCharArray();
//for printing the puzzle
for(int j=0;j<m.length();j++)
{
if(mj[j]%3==0)
{
System.out.print(" "+mj[j]);
}
else System.out.print(" ___ ");
}
System.out.println();
//taking the input from user
Scanner scanner=new Scanner(System.in);
do{
char c=scanner.next().charAt(0);
System.out.println(c+"-----scanning complete");
for(int i=0;i<m.length();i++)
{
if(c==mj[i])
{
String n= ?????????????????????;
counter--; }
}while(counter != 0);
}}
答案 0 :(得分:1)
您需要保存播放器正确的某种字符数组。 尝试这样的事情:
import java.io.*;
import java.util.Scanner;
class hangman {
public static void main(String args[]){
int counter = 6;
String m = "ashish";
char mj[] = m.toCharArray();
char correct[] = new char[mj.length];
//for printing the puzzle
for(int j=0;j<m.length();j++) {
if(mj[j]%3==0) {
System.out.print(" "+mj[j]);
correct[j] = mj[j];
}
else {
System.out.print(" ___ ");
}
}
System.out.println();
//taking the input from user
Scanner scanner=new Scanner(System.in);
do {
char c=scanner.next().charAt(0);
System.out.println(c+"-----scanning complete");
for(int i=0;i<m.length();i++) {
if(c==mj[i]) {
correct[i] = c;
counter--;
}
// This is the default value of a char in Java.
if (correct[i] == '\u0000') {
System.out.print(" ___ ");
} else {
System.out.print(correct[i]);
}
}
} while(counter != 0);
}
}