我不确定为什么我的Java代码不会编译,任何建议将不胜感激.
String rank = card.substring(0,1);
String suit = card.substring(1);
String cards = "A23456789TJQKDHSCl";
String[] name = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Diamonds","Hearts","Spades","Clubs"};
String c ="";
for(int a = 0, b = 1; a<cards.length-1; b=a+1; a++;){
if(rank===cards.substring(a,b){
c+=name[a];
}
}
system.out.println(c);
Run Code Online (Sandbox Code Playgroud)
And*_*ter 21
它是cards.length(),不cards.length(length是一种方法的java.lang.String,而不是一个属性).
它是System.out(资本''),而不是system.out.请参阅java.lang.System.
它是
for(int a = 0, b = 1; a<cards.length()-1; b=a+1, a++){
Run Code Online (Sandbox Code Playgroud)
不
for(int a = 0, b = 1; a<cards.length-1; b=a+1; a++;){
Run Code Online (Sandbox Code Playgroud)从语法上讲,它if(rank == cards.substring(a,b)){不是if(rank===cards.substring(a,b){(双等于,不是三等于;缺少右括号),但要比较两个字符串是否相等需要使用equals():if(rank.equals(cards.substring(a,b))){
您应该考虑下载Eclipse,它是一个用于Java开发的集成开发环境(不仅仅是).Eclipse会在您键入时显示错误,并提供修复这些错误的帮助.这使得开始Java开发变得更加容易.
循环中只允许使用两个分号for。
如果需要对多个变量进行初始化或操作多个变量,可以通过用逗号(,)分隔来实现。
for(int i=0, j=5; i < 5; i++, j--)
Run Code Online (Sandbox Code Playgroud)
注意:不允许使用逗号分隔多个条件。
for(int i=0, j=5; i < 5, j > 5; i++, j--) // This is NOT allowed.
Run Code Online (Sandbox Code Playgroud)
而不是这个:
for(int a = 0, b = 1; a<cards.length-1; b=a+1; a++;){
它应该是
for(int a = 0, b = 1; a<cards.length()-1; b=a+1, a++){
^ ^ ^
| | |
| | |
-------------------------------------------Note the changes
|
v |
if(rank==cards.substring(a,b){ |
-------------------------------------------------------------
|
v
System.out.println(c); //capital S in system
Run Code Online (Sandbox Code Playgroud)
你的 for 循环是错误的。尝试 :
for(int a = 0, b = 1; a<cards.length()-1; b=a+1, a++){
Run Code Online (Sandbox Code Playgroud)
另外,System代替system和==代替===.
但我不确定你想做什么。