- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这段代码我遇到了一些问题。我做的第一件事显然是创建了一些数组,这些数组将保存每个联系人的所有信息,因为我正在构建一个基本的联系人管理器。我的主要方法只是调用我的菜单方法来启动序列。在我的菜单中,用户可以选择他们想做什么。他们选择哪个选项将取决于他们在键盘上输入的数字。反过来,这将激活不同的方法。
我遇到的问题如下:
按“1”(查看所有联系人)后,计算机会吐出 100 个空值或 100 次重复我上次在“2”中输入的任何内容,添加联系人。
虽然是的,我确实希望我的菜单在某个操作发生后自动重复,但它会立即执行。例如,在我按下“1”后所有重复发生后,它会直接返回主菜单,并且很难以这种方式阅读所有内容。
import java.io.IOException;
import java.util.Scanner;
``public class MainHub {
// global variables that will be needed
static String[] name = new String[100];
static String[] number = new String[100];
static String[] email = new String[100];
static String[] address = new String[100];
static String[] birthday = new String[100];
static String[] nickname = new String[100];
static int x;
public static int counter = 0;
static Scanner in = new Scanner(System.in);
public static void main (String[] args) throws IOException{
menu(); // call menu method to start program
}
//Holds all the information for the menu
public static void menu() throws IOException{
int choice = -1; //start at -1 as to not compete with an index number
while (choice != 7){ //while loop to get a choice from user
//telling the user what everything is, simple output message
System.out.println("\t Main Menu\n");
System.out.println("Enter the corrosponding number for what you want to do.\n");
System.out.println("1.View all contacts\n2.Add contact\n3.Edit contact\n4.Delete contact\n5.Save contact list\n6.Load contact list\n7.Exit");
choice = Integer.valueOf(in.nextLine()).intValue(); //this allows the user input to be read and used to make a choice
switch(choice){
case 1: viewAllContacts(); //if user inputs 1, call method to view all the contacts
break; //stop the loop
case 2: addContact(); //if user inputs 2, call method to add a contact
break; //stop the loop
case 3: editContact(); // if user inputs 3, call method to view contacts and choose one to edit
break; //stop the loop
case 4: deleteContact(); // if user inputs 4, call method to view contacts and choose one to delete
break; //stop the loop
case 5: saveContact(); // if user inputs 5, call method to save current contact list into a text file
break; //stop the loop
case 6: loadContact(); // if user inputs 6, call method to load a text file to input contacts into array
break; //stop the loop
case 7: System.out.println("You are exiting"); // if user inputs 7, tell user he is leaving
break; //stops the loop
default: System.out.println("That is not one of the options."); // if user doesn't input one of above numbers, it will tell user not an option
}
}
}
//holds information, once called upon the user will be able to see all their contacts
public static void viewAllContacts(){
while(counter<100){ //while the counter has less than 101 it will print out the full list of contacts
if(counter>0){ //if counter is greater than 0 print list of contacts
System.out.println("Full name: " +name[x]);
System.out.println("Number: " +number[x]);
System.out.println("E-mail Address: " +email[x]);
System.out.println("Home Address: " +address[x]);
System.out.println("Birthday: " +birthday[x]);
System.out.println("Nickname: " +nickname[x]);
System.out.println(" "); //space so that way the contact list is a bit prettier
counter++;
}else{
System.out.println("There are no contacts in your list yet."); //else tell user there is no contacts
}
}
}
//lets the user add a contact and all the information
public static void addContact() throws IOException{
//as long as the counter is less than 101 you can add contacts
if(counter<100){
System.out.println("Enter Contact's Full Name: "); //allows user to add a name to contact
name[x] = in.nextLine(); //whatever is typed will be added into name variable
System.out.println("Enter Contact's Number: "); //allows user to add a number to contact
number[x] = in.nextLine(); //whatever is typed will be added into number variable
System.out.println("Enter Contact's E-mail Address: "); //allows user to add an E-mail address to contact
email[x] = in.nextLine(); //whatever is typed will be added into email variable
System.out.println("Enter Contact's Home Address: "); //allows user to add a home address to contact
address[x] = in.nextLine(); //whatever is typed will be added into address variable
System.out.println("Enter Contact's Birthday: "); //allows user to add a birthday to contact
birthday[x] = in.nextLine(); //whatever is typed will be added into birthday variable
System.out.println("Enter Contact's Nickname: "); //allows user to add a nickname to contact
nickname[x] = in.nextLine(); //whatever is typed will be added into nickname variable
counter++; //adds 1 to counter to allow space for next contact in arrays
}else{
System.out.println("Sorry, the contact list is full."); //if counter is 101 it will print the contact list is full
}
System.out.println("Your contact has been added");
}
最佳答案
问题是您使用 x
作为数组的索引,但是 x
永远不会递增。
当你递增counter
尝试
System.out.println("Full name: " +name[counter]);
与添加联系人
整理你的索引。
关于java - 我在创建基本联系人管理器时遇到问题。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30608619/
如何将十进制数字转换为mixed radix表示法? 我猜想给定每个基数数组的输入和十进制数,它应该输出每列值的数组。 最佳答案 伪代码: bases = [24, 60, 60] input = 8
我有 Table-A,其中有“x”行。 (对于这个例子有 8 行) 我通过使用游标创建了列数为“x”的Table-C。 (使其动态化;如果将更多行添加到 Table-A,则会在 Table-C 中创建
我有一个关于对象的(很可能是简单而愚蠢的)问题。我创建了实例“Person”的对象“jon”。当我打电话时 console.log(jon.name) 控制台会给我输出“jon”。到目前为止,一切都很
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: javascript function vs. ( function() { … } ()); 抱歉,如果这太基础了
我正在尝试用 Java 重新创建射弹轨迹,但是,我遇到了一些问题。我看过很多解释公式之类的视频,但他们的方程中有一个目标,而我没有。我的意思是,他们有一个范围来计算子弹的下落,但我试图弄清楚子弹最终会
(希望如此)来自一个完整的 Rust 初学者的一个简单问题。我的循环有什么问题? num 计算结果为“69”的速度相当快,但是一旦 num 设置为“69”,循环就永远不会退出。我肯定遗漏了一些明显的东
我在 id="name"的元素上应用“.length”,但它计数为 29 而不是 14。我想知道我的错误在哪里?如果有人可以让我知道,那就太好了。谢谢! var name=document.getEl
我知道这很简单,但由于某种原因我无法让它工作。我正在尝试在 Java 中创建自定义颜色,但它似乎不起作用。 import java.awt.Color; Color deepGreen = new C
我有一个大文件,其中每一行都包含一个子字符串,例如 ABC123。如果我执行 grep ABC file.txt 或 grep ABC1 file.txt 我按预期返回这些行,但如果我执行 grep
我想将以下实体映射转换为 Priority 对象。在 getter 上,当我将“Short”更改为“Priority”并遵循 this.priority 时,它会提示 'basic' 属性类型不应该是
我正在开发一个相当基本的函数,我发现很难弄清楚为什么我会得到我的输出。 def mystery(n): print(n) if n < 4: my
我正在尝试对 WordPress 安装的新闻部分实现同位素过滤。我是 JavaScript/jQuery 的新手,正在尝试随时随地学习。我首先使用 Filters section of the Iso
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我在另一个实体类中引用一个实体并收到此错误。下面是示例代码。我在 persistence.xml 中也有这些类。 是什么导致了这个问题?我正在使用 Spring 数据 JPA 和 Hibernate。
我正在解析 HTML 并重新格式化图像以使其更好地适应。由于某种原因,当我有多个图像需要解析时,我会超出范围,而且我一生都无法弄清楚为什么。 当 imgArray.count >1 时,我将使用带有递
我是 SQL 新手,正在尝试创建一个基本的子查询。我需要找出经理的平均年龄和实习生的平均年龄之间的差异。 标题为一栏 - 经理或实习生年龄是一列,全部在同一个表中。 我会使用两个子查询来做类似的事情:
我习惯了 csh,所以不得不使用 bash 有点烦人。这段代码有什么问题? if[$time > 0300] && [$time 和 300 && time < 900 )) then mod
我建立了这个页面:http://excelwrestling.com/poola.php即将到来的双重锦标赛。我的大部分数据都是从我的 mySQL 数据库中提取的,现在只有一些示例数据。 我希望链接选
是否有任何原因导致以下内容不起作用: for (i=0;i < someArray.length;i++) { if (someArray[i].indexOf("something") !=
我现在正在学习 Javascript,有一个问题一直困扰着我! 因此,我在这里所需要做的就是在此输入框中键入颜色,单击按钮并将标题更改为键入的颜色(仅当键入的颜色位于变量中指定的数组中时)。 我的代码
我是一名优秀的程序员,十分优秀!