Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要我的程序知道我在哪里停了吗_Java_Loops_Memory_Continue - Fatal编程技术网

Java 需要我的程序知道我在哪里停了吗

Java 需要我的程序知道我在哪里停了吗,java,loops,memory,continue,Java,Loops,Memory,Continue,所以我正在尝试制作剪辑卡应用程序。换句话说,我正在制作一个应用程序,计算客户购买了多少咖啡,每购买10次,客户将获得一次免费咖啡。我已经基本完成了循环,但是我很难弄清楚如何让我的程序记住我停止的地方。例如,假设我买了第七杯咖啡,准备离开,所以我想关闭应用程序;有没有办法让程序记住下次运行时在哪里继续 以下是我目前掌握的情况: import java.util.Scanner; public class FelixNeww { public static void main(String []

所以我正在尝试制作剪辑卡应用程序。换句话说,我正在制作一个应用程序,计算客户购买了多少咖啡,每购买10次,客户将获得一次免费咖啡。我已经基本完成了循环,但是我很难弄清楚如何让我的程序记住我停止的地方。例如,假设我买了第七杯咖啡,准备离开,所以我想关闭应用程序;有没有办法让程序记住下次运行时在哪里继续

以下是我目前掌握的情况:

import java.util.Scanner;

public class FelixNeww {

public static void main(String [] args) {
    Scanner key;
    String entry;
    int count = 0;
    String password = "knusan01";
    while(true) {
        System.out.println("Enter password: ");
        key = new Scanner(System.in);
        entry = key.nextLine();
        if(entry.compareTo(password) == 0){
            count++;
            System.out.println("You're one step closer to a free coffe! You have so far bought " 
            + count + " coffe(s)");
        }
        if(count == 10  && count != 0){
            System.out.println("YOU'VE GOT A FREE COFFE!");
            count = 0;
        }
        if(entry.compareTo(password) != 0){
            System.out.println("Wrong password! Try again.\n");
        }
    }
}

}

谢谢

如果要确保保存进度,请尝试查看RuntimeHook,如下所述:

您需要做的是将数据存储在一个文件中,即当前计数。使用以下代码可以相当轻松地完成此操作:

public void saveToFile(int count)
{
    BufferedWriter bw = null;
    try
    {
        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("countStorage"))));
        bw.write(count);
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if(bw != null)
        {
            try
            {
                bw.close();
            }
            catch(IOException e) {}
        }
    }
}

public int readFromFile()
{
    BufferedReader br = null;
    try
    {
        br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("countStorage"))));
        String line = br.readLine();
        int count = Integer.parseInt(line);
        return count;
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if(br != null)
        {
            try
            {
                br.close();
            }
            catch(IOException e) {}
        }
    }
}

坚持就是这个词。您可以使用数据库存储信息。写入文件并将其存储在硬盘上。。我认为我们不需要数据库来处理这个问题。其他注释,您不必同时比较count==10&&count!=0,只需计数==10。您还可以使用entry.equals(密码)。您可以将you got a free coffe if statement(您得到了一个“free coffe if statement”(免费的coffe if语句)放在第一个if语句中。一般来说,将数据持久化到文件具有简单性的优点(只需读写一个新文件),数据库在效率、安全性和大数据集方面都更好,尽管它们需要更多的理解,并且有自己的语言。如果没有其他的经验,我会建议从文件选项开始。所以我对这些东西非常陌生。我该如何准确地做到这一点,也就是说,我该如何写入一个文件,并使我的程序在每次再次运行时读取它??