import java.util.*;
public class Assigement1{
private static Object Start(int n,int c,String q){
Random myRandomizer=new Random();
n=Math.abs(myRandomizer.nextInt()%1000)+1;
int b=0;
int counter=0;
for(int m=1;m>0;){
System.out.println("input your name please:");
Scanner name=new Scanner(System.in);
q=name.next() ;
if (q.matches("^[a-zA-Z]*")){
break;
}
else{
System.out.println("Input error,please input your name again");
m=1;
}
}
A:while(b==0){
int b1=0;
B:while(b1==0){
System.out.println("input a guess number");
Scanner In=new Scanner(System.in);
String w=In.nextLine();
counter+=1;
c=counter;
if(w.equals("n")){
break A;
}
int I=0;
if (w.matches("^\\d+$")) {
I=Integer.parseInt(w);
}
else{
System.out.println("Input error,please input a number again or input 'n' to stop the game");
break B;
}
if(I>n){
System.out.println("Your guess is higher,guess again or input 'n' to stop the game");
continue;
}
if(I<n){
System.out.println("Your guess is lower,guess again or input 'n' to stop the game");
continue;
}
if(I==n){
System.out.println("Nice guess");
break A;
}
}
if(c>10){
System.out.println("Stupid player,your IQ is not enough to play this game");
break A;
}
}
return (q);
}
public static void main(String[] args){
int n=0;
int c=0;
int s=0;
int x=0;
String q = null;
String sc;
String ScoreList[][]=new String[10][2];
for(int i=0;i<ScoreList.length;i++){
int j=1;
sc=String.valueOf(x);
ScoreList[i][j]=sc;
}
for(int a=1;a>0;){
Start(n,c,q);
System.out.println("Play again?:yes=1 No=0");
Scanner d=new Scanner(System.in);
a=d.nextInt();
}
s=1000-c;
int s3=Integer.parseInt(ScoreList[9][1]);
if(s>s3){
sc=String.valueOf(s);
ScoreList[9][1]=sc;
ScoreList[9][0]=q;
for(int i=8;i>-1;i--){
int s1=Integer.parseInt(ScoreList[i][1]);
if(s>s1){
String s2=String.valueOf(s1);
String temp=ScoreList[i+1][1];
ScoreList[i+1][1]=s2;
ScoreList[i][1]=temp;
String temp2=ScoreList[i+1][0];
ScoreList[i+1][0]=ScoreList[i][0];
ScoreList[i][0]=temp2;
}
else break;
}
/*for(int i=9;i>-1;i--){
for(int j=8;i>-1;j--){
int s1=Integer.parseInt(ScoreList[i][1]);
int s2=Integer.parseInt(ScoreList[j][1]);
if (s1> s2) {
ScoreList[i][1] = String.valueOf(s1);
ScoreList[j][1] = String.valueOf(s2);
String temp1 = ScoreList[i][1];
ScoreList[j][1] =ScoreList[i][1] ;
ScoreList[i][1] = temp1;
String temp2 =ScoreList[i][0];
ScoreList[j][0] =ScoreList[i][0] ;
ScoreList[i][0] = temp2;
}
else{
break;
}
}
}
}*/
for(int i=0;i<ScoreList.length;i++){
for(int j=0;j<ScoreList[i].length;j++){
System.out.println(ScoreList[i][j]);
}
}
System.out.println(c+q);
}
}
}小弟遇都了几个问题:1.如何在外层函数start里 return n,a,q 他们的类型不同 不知道怎么写
2. 主函数输出c,q,a都是空在这个阶段我至少应该是返回了q的吧但是还是输出null。
3.我想使用二维数组输出一张游戏结果 包含名字和分数的,不知道这个数组排序有问题没。
4.不知道ARRAYLIST 怎么声明是在MAIN函数以外还是?
public class Assigement1{
private static Object Start(int n,int c,String q){
Random myRandomizer=new Random();
n=Math.abs(myRandomizer.nextInt()%1000)+1;
int b=0;
int counter=0;
for(int m=1;m>0;){
System.out.println("input your name please:");
Scanner name=new Scanner(System.in);
q=name.next() ;
if (q.matches("^[a-zA-Z]*")){
break;
}
else{
System.out.println("Input error,please input your name again");
m=1;
}
}
A:while(b==0){
int b1=0;
B:while(b1==0){
System.out.println("input a guess number");
Scanner In=new Scanner(System.in);
String w=In.nextLine();
counter+=1;
c=counter;
if(w.equals("n")){
break A;
}
int I=0;
if (w.matches("^\\d+$")) {
I=Integer.parseInt(w);
}
else{
System.out.println("Input error,please input a number again or input 'n' to stop the game");
break B;
}
if(I>n){
System.out.println("Your guess is higher,guess again or input 'n' to stop the game");
continue;
}
if(I<n){
System.out.println("Your guess is lower,guess again or input 'n' to stop the game");
continue;
}
if(I==n){
System.out.println("Nice guess");
break A;
}
}
if(c>10){
System.out.println("Stupid player,your IQ is not enough to play this game");
break A;
}
}
return (q);
}
public static void main(String[] args){
int n=0;
int c=0;
int s=0;
int x=0;
String q = null;
String sc;
String ScoreList[][]=new String[10][2];
for(int i=0;i<ScoreList.length;i++){
int j=1;
sc=String.valueOf(x);
ScoreList[i][j]=sc;
}
for(int a=1;a>0;){
Start(n,c,q);
System.out.println("Play again?:yes=1 No=0");
Scanner d=new Scanner(System.in);
a=d.nextInt();
}
s=1000-c;
int s3=Integer.parseInt(ScoreList[9][1]);
if(s>s3){
sc=String.valueOf(s);
ScoreList[9][1]=sc;
ScoreList[9][0]=q;
for(int i=8;i>-1;i--){
int s1=Integer.parseInt(ScoreList[i][1]);
if(s>s1){
String s2=String.valueOf(s1);
String temp=ScoreList[i+1][1];
ScoreList[i+1][1]=s2;
ScoreList[i][1]=temp;
String temp2=ScoreList[i+1][0];
ScoreList[i+1][0]=ScoreList[i][0];
ScoreList[i][0]=temp2;
}
else break;
}
/*for(int i=9;i>-1;i--){
for(int j=8;i>-1;j--){
int s1=Integer.parseInt(ScoreList[i][1]);
int s2=Integer.parseInt(ScoreList[j][1]);
if (s1> s2) {
ScoreList[i][1] = String.valueOf(s1);
ScoreList[j][1] = String.valueOf(s2);
String temp1 = ScoreList[i][1];
ScoreList[j][1] =ScoreList[i][1] ;
ScoreList[i][1] = temp1;
String temp2 =ScoreList[i][0];
ScoreList[j][0] =ScoreList[i][0] ;
ScoreList[i][0] = temp2;
}
else{
break;
}
}
}
}*/
for(int i=0;i<ScoreList.length;i++){
for(int j=0;j<ScoreList[i].length;j++){
System.out.println(ScoreList[i][j]);
}
}
System.out.println(c+q);
}
}
}小弟遇都了几个问题:1.如何在外层函数start里 return n,a,q 他们的类型不同 不知道怎么写
2. 主函数输出c,q,a都是空在这个阶段我至少应该是返回了q的吧但是还是输出null。
3.我想使用二维数组输出一张游戏结果 包含名字和分数的,不知道这个数组排序有问题没。
4.不知道ARRAYLIST 怎么声明是在MAIN函数以外还是?
2、值传递的问题。q就应该是null哦,这个好改一点,直接在main里面q=Start(..);即可
3、。。没看明白
4、ArrayList声明在main方法里面吧。。虽然我不知道是用来干嘛的。。 夜深人静的。。大概看了看 回帖接分。。