Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.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/3/arrays/13.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 如何打印用户创建的现有配置文件?在用户创建的两个配置文件之间切换有什么提示吗? 类配置文件 { 私人扫描仪sc; 个人资料[]个人资料; private int idx;//配置文件数组的主索引 private int nop;//配置文件的数量 公众简介() { sc=新扫描仪(系统英寸); idx=-1; nop=0; 配置文件=新配置文件[3]; } public void createProfile(字符串第一,字符串最后,整数年龄) { 如果(idx_Java_Arrays - Fatal编程技术网

Java 如何打印用户创建的现有配置文件?在用户创建的两个配置文件之间切换有什么提示吗? 类配置文件 { 私人扫描仪sc; 个人资料[]个人资料; private int idx;//配置文件数组的主索引 private int nop;//配置文件的数量 公众简介() { sc=新扫描仪(系统英寸); idx=-1; nop=0; 配置文件=新配置文件[3]; } public void createProfile(字符串第一,字符串最后,整数年龄) { 如果(idx

Java 如何打印用户创建的现有配置文件?在用户创建的两个配置文件之间切换有什么提示吗? 类配置文件 { 私人扫描仪sc; 个人资料[]个人资料; private int idx;//配置文件数组的主索引 private int nop;//配置文件的数量 公众简介() { sc=新扫描仪(系统英寸); idx=-1; nop=0; 配置文件=新配置文件[3]; } public void createProfile(字符串第一,字符串最后,整数年龄) { 如果(idx,java,arrays,Java,Arrays,将问题分解为步骤 打印带有索引的数组 征求意见 处理输入 System.out.println(“选择配置文件”); 对于(int i=0;i

将问题分解为步骤

  • 打印带有索引的数组
  • 征求意见
  • 处理输入

System.out.println(“选择配置文件”);
对于(int i=0;i=0){
idx=下一个;
}否则{
//无效索引
}
但是,如果在执行此操作后调用addprofile方法,最终将覆盖数组中的数据


为了避免这种情况,您需要在数组中只向空位置添加配置文件

可能类似于(Profile p:profiles){System.out.println(“Name=%s%s,age%d\n”,p.getFirstName,p.getLastName(),p.getAge);?作为一种通用样式,我建议不要在数组中使用变量名
nop
“配置文件数”,因为有一个更常见的意思是“无操作”“来自汇编代码。相反,我建议使用仍然简短但明确的名称
numProfiles
profiles
变量声明在哪里?调用这些方法的代码在哪里?您如何使用它们?回过头来,我收回了以前的建议,现在建议使用类似于
的方法。”(int-px=0;px
oops,忘记包括:private Profile[]profiles;@cricket
class Profiles
{
   private Scanner sc;
   private Profile[] profiles;
   private int idx; // master index of profiles array
   private int nop; //number of profiles
public Profiles()
{
   sc = new Scanner(System.in);
   idx = -1;
   nop = 0;
   profiles = new Profile[3];
}
public void createProfile(String first,String last,int age)
{
    if(idx<profiles.length-1)
    {
        Profile p = new Profile(first,last,age);
        idx++;
        profiles[idx] = p;
        nop++;
        Util.print("Profile has been created\n");
    }
    else
    {
        Util.print("No room to create a new profile\n");
    }
}
public void switchProfile()
{
  if(nop==0)
  {
    Util.print("Unable to switch profiles");
    return;
  }else
   {
   //print the idx and all profiles that currently exist <--not sure how to do this
   //accept a profile idx that the user chooses (use Scanner to get user input)
   //set idx to the input (this will direct the user to the profile idx they chose)
   }
}
System.out.println("Pick a profile");
for (int i = 0; i < profiles.length; i++) {
    System.out.printf("%d : %s\n", i, profiles[i].getName());
} 

int next = Integer.parseInt(sc.nextLine());
if (next < profiles.length && next >= 0) {
    idx = next;
} else {
  // invalid index 
}