java - 创建约会中心程序,但无法将变量保存到循环之外
问题描述
这是我到目前为止所写的:
import java.util.Scanner;
public class appointment {
public static void main(String[]args) {
int input;
int appointment = 0;
String name;
int time;
do {
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to ACEPrep Appointment Center! How can we help you?
(1. Make an appointment 2. Check Appointment Details 3. Check Number of Appointments 4. Leave");
input=Integer.parseInt(sc.nextLine());
if(input==1) {
System.out.println("Name:");
name=sc.nextLine();
boolean name2=true;
if(name2==true) {
System.out.println("Select a time between 1-8 PM:");
time=sc.nextInt();
System.out.println("Saved! You have an appointment at " + time +" PM with " + name);
appointment++;
}
}
if(input==2) {
do{
System.out.println("You have an appointment with " + name + " at " + time);
}while(appointment<appointment+1);
}
if(input==3) {
System.out.println("You have " + appointment + " appointment(s).");
}
if(input==4) {
System.out.println("Thank you for using us! Goodbye!");
break;
}
}while(input<4);
}
}
对于具有 的行System.out.println("You have an appointment with " + name + " at " + time);
,它表示由于没有值而无法调用变量,并且我想将第一个循环中的变量值保存到原始变量中,但我不知道该怎么做。请问我能得到一些帮助吗?
解决方案
你必须初始化:
String name = ""; int time = 0;
这也是一个无限循环do{...}while(appointment<appointment+1);
推荐阅读
- jquery - 如何在 React 应用程序的窗口中运行 jQuery 应用程序
- ios - 如何在 iOS 14.4+ 设备上触发系统诊断
- sql-server - SQL Server:一行中的案例返回值
- android - 如何在flutter中使用google calendar watch api方法?
- kotlin - Spring Cloud Stream Reactive - 处理消息异常
- sql - 为什么 Oracle 中的嵌套聚合函数需要 GROUP BY
- node.js - 使用 ws 发送自定义事件
- clickjacking - 有没有办法阻止通过移动应用程序的 webView 查看我的网站?
- python - 下载了一个jupyter笔记本,但它不会打开
- javascript - 在另一个 API 调用之前清除 api 响应数组