Question :This a program for snake and ladder.the user enters the number of players and players name.


-程序不是从调用throwdice()方法的位置执行的。

package practical1;

import java.util.ArrayList;
import java.util.Scanner;

public class snakeandladder1 {

int totalpos=100;
int v;
int[] scores = new int[100];
int score=0;
int l=0;

public int throwdice() //to calculate the dice value
{
    int i=0;
    {
        while(i==0)
        {
            i=(int)Math.random()*100;
            i=i%13;
        }
        return i;
    }
}

public int ladder(int score) //to check if the user has reached a ladder
{
    if(score==15)
        score=30;
    else if(score == 45)
        score=71;
    else if(score == 25)
        score=62;
    else if(score == 81)
        score=91;
    else if(score == 9)
        score=39;
    scores[l]=score;
    return score;
}

public int snake(int score) //to check if the user has reached a snake
{
    if(score == 29)
        score=11;
    else if(score == 81)
        score=48;
    else if(score == 92)
        score=71;
    else if(score == 30)
        score=6;
    else if(score == 58)
        score=19;
    scores[l]=score;
    return score;
}

void start()
{
    System.out.println("Enter the number of players:");
    Scanner in=new Scanner(System.in);
    int n=in.nextInt();
    System.out.println("Enter Players names in order:");
    ArrayList<String> name1=new ArrayList<String>(); //an array to store players names
    for (int i=0;i<n;i++)
    {
        Scanner in1=new Scanner(System.in);
        String name2=in1.nextLine();
        name1.add(name2);
    }
    while(true)
    {
        while(l<n) //for each player
        {
            System.out.println("Click y to roll dice");
            Scanner in2=new Scanner(System.in);
            String yes=in2.nextLine();
            if (yes == "y")             //------!!!This part is not getting executed
            {                           // It is taking y but not going further.It does not print
                v=throwdice();          //anything also
                System.out.println("dice value:"+v);
            }
            score = scores[l]+v;        //the value of dice is added to individual score
            if(score==totalpos)         //checking if reached 100
            {
                System.out.println("User:"+name1.get(l)+" got "+v+".Winner!!!");
                break;
            }
            else if(score > totalpos) //checking if dice value is more than required to 100
            {
                scores[l]=scores[l];
            }
            int s1;
            s1=ladder(score); //ladder is called, if true valuue is added to score[i] in the function itself
            if(s1==score)
            {
                snake(score); //if not ladder then snake is called
            }
            System.out.println("Current score of "+name1.get(l)+" is:"+scores[l]);  //to display result
            l++;
        }
        if(l==n)
        {
            l=0;
        }
    }
}
public static void main(String[] args) throws Exception{
    // TODO Auto-generated method stub
    snakeandladder1 sal=new snakeandladder1();
    sal.start();
}


}

我得到的输出:

输入玩家人数:
2
按顺序输入玩家名称:
抹布

单击y掷骰子
ÿ
抹布的当前得分是:0
单击y掷骰子
ÿ
kis当前得分是:: 0
单击y掷骰子
ÿ
抹布的当前得分是:0
单击y掷骰子

请帮忙 ..

最佳答案

您的代码中存在一些问题...首先,我建议您坚持正确的Java用法,最佳实践,命名约定,异常处理。.从您的类名开始。 。

关于你的问题
1:



if (yes == "y") {}





应该更改为:



if (yes.equalsIgnoreCase("y")) {}





在这种情况下,我使用“ rert588”的上述答案。

2:throwdice()方法中有几个问题。我看到您当前的方法进入永无休止的while循环,因为我始终为“ 0”。我不清楚您要实现什么目标,但我认为应该像下面这样。

应该更改为:



public int throwdice() //to calculate the dice value
  {
    int i = (int)(Math.random() * 100); // make note on your Casting.
    i = i % 13;
    return i;
  }





尝试一下,希望对您有所帮助。

07-24 22:23