[英]The Logic of code is not being executed
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按顺序输入玩家名称:rag kis单击y掷骰子y rag的当前得分是:0点击y掷骰子y kis的当前得分是:0 y掷骰子y当前得分rag是:0单击y掷骰子
请帮忙 ..
而不是输入:
if(yes == "y"){
}
尝试输入:
if(yes.equalsIgnoreCase("y"){
}
它将起作用。原因是yes是String类的一个对象,它是一个intern类。 可以在调用String类的方法时用作对象,但与StringBuilder类相比,它具有一些限制。
您的代码中存在一些问题...首先,我建议您坚持正确的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; }
尝试一下,希望对您有所帮助。
通常,在字符串比较中使用==时,它不会比较是否相等,而是会检查它是否是同一对象。 因此在比较字符串时使用.equals。 在这里谈论:
而不是使用“ ==”比较String,应该使用String类的equals或equalsIgonreCase方法。
但是,如果有人不小心给了其他一些值(通常在“ y”或“ n”之前或之后),这仍然可能引起问题。
因此,您应该比较字符而不是比较字符串,这可以使用“ ==”轻松完成。
if (yes == "y") // Please note that this will never be true
使用"y".equals(yes)
代替。
对于String
, ==
测试引用相等性,这意味着其中两个变量应指向相同的内存位置。 在这种情况下,变量yes
和常量“ y”不在同一内存中。
.equals()
测试值是否相等,已被String
覆盖。 请注意,对于大多数类, .equals()
与==
相同,除非它被重写
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.