我在更改变量的值时遇到问题。我找到了This个较旧的问题,但在将其应用于我的代码时仍然遇到问题。如果有人可以查看我的代码并提供指导,我将非常感激。这是我的代码:

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();

    }


我添加它只是为了测试修改后的错误是否有效。

    System.out.println(GMTime.getError());


    }//end of main


} //课程结束

我想做的是,如果“时间”中不存在“冒号”,请更改“错误”的值,以便以后可以从驱动程序中打印出来。

最佳答案

GMTime和ColonCheck()中至少存在三个问题:


GMTime中的变量似乎不需要为static
while (time.contains(":"))循环可以是无限循环。
不管if (colon != 1 || colon != 2)是什么值,语句colon实际上始终为true。


解决这些问题的一种可行方法如下(我已经测试了代码,因此请尝试从这里开始并完成整个程序):

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

07-26 02:54