我在更改变量值时遇到问题。我发现了This较早的问题,但我仍然无法将其应用到我的代码中。如果有人可以查看我的代码并提供一些指导,我将非常感激。这是我的代码:
.UserItem initWithCoder:]: unrecognized selector sent to instance 0x7fd4714ce010
司机:
public class GMTime {
private static String time;
private static int hour;
private static int minutes;
private static int colon;
private static String error = null;
// **********************************************************
// constructor passing time as a string
public GMTime(String temp) {
time = temp;
}
// end constructor
// **********************************************************
// method checking the colon presence and place between 1-2 index
public String colonCheck(String error) {
while (time.contains(":")) {
colon = time.indexOf(":");
} // end of while
if (colon != 1 || colon != 2) {
error = "Invalid separator entered";
}
System.out.println(error);
return error;
} // end colon check
public static String getError(){
return error;
}
}
我添加了这个只是为了测试修改后的错误是否有效。
import java.util.Scanner;
public class GMUnit6Ch15{
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
String time;
System.out.print("Enter time in the form mm:dd (\"q\" to quit) :");
time = stdIn.next();
while (!time.equalsIgnoreCase("q")){
GMTime userTime = new GMTime(time);
System.out.print("Enter time in the form mm:dd (\"q\" to quit) :");
time = stdIn.next();
}
} //班级的结尾
我想做的是"冒号"在"时间"中不存在改变"错误"的值这样我以后就可以从司机那里打印出来了。
答案 0 :(得分:0)
GMTime和colonCheck()至少有三个问题:
GMTime
中的变量似乎不一定是static
。while (time.contains(":"))
循环可能是无限循环。if (colon != 1 || colon != 2)
实际上始终为真,无论colon
是什么价值。解决这些问题的一种合理方法如下(我已经测试了代码,所以请尝试从这里开始并完成整个程序):
GMTime课程:
public class GMTime {
private String time;
private int hour;
private int minutes;
private int colon;
private String error = null;
// **********************************************************
// constructor passing time as a string
public GMTime(String temp) {
time = temp;
}
// end constructor
// **********************************************************
// method checking the colon presence and place between 1-2 index
public void colonCheck() {
while (time.contains(":")) {
colon = time.indexOf(":");
time = time.substring(colon + 1);
} // end of while
if (colon != 1 && colon != 2) {
error = "Invalid separator entered";
} else {
error = "No error";
}
} // end colon check
public String getError() {
return error;
}
}
GMUnit6Ch15课程:
import java.util.Scanner;
public class GMUnit6Ch15 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
String time;
System.out.print("Enter time in the form mm:dd (\"q\" to quit) :");
time = stdIn.next();
while (!time.equalsIgnoreCase("q")) {
GMTime userTime = new GMTime(time);
userTime.colonCheck();
System.out.println(userTime.getError());
System.out.print("Enter time in the form mm:dd (\"q\" to quit) :");
time = stdIn.next();
}
}
}
示例输入/输出:
以mm:dd(“q”退出)的形式输入时间:1111
输入的分隔符无效
以mm:dd(“q”退出)的形式输入时间:11:11
没有错误
以mm:dd(“q”退出):q
的形式输入时间