java - 循环和封装的使用

标签 java encapsulation

我正在尝试让程序执行以下操作: 如果输入的数据不被接受,请再次请求信息 (注意:可以再次请求所有信息,不必只请求重新输入特定信息,但如果您愿意,也可以)。

对于程序来说,一切似乎都运行良好,除了当它的分辨率时,它会要求另一个输入,但如果输入不正确,它就会接受。我需要它继续运行,直到输入正确的输入。

import java.util.Scanner;
public class encap 
{
    private static String userID;
    private static String password;
    private static String resolution;
    private static int Ramsize;
    private static int freespace;
    private static int videocard;

    //Get and Set methods

    public static String getuserID()
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter userID : ");
        userID = input.next();
        return userID;
    }
    public static String getpassword()
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter password : ");
        password = input.next();
        return password;
    }
    public static String getresolution()
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter video resolution: ");
        resolution = input.next();
        if (resolution.equals("800x600") || resolution.equals("1024x768") || resolution.equals("1152x900"));
        else
        {
            while(true)
            {
                System.out.println("Information invalid, Please fill again");
                String getresolution = input.next();
                if (resolution.equals("800x600") || resolution.equals("1024x768") || resolution.equals("1152x900"));
                break;
            }
        }
        return resolution;
    }

    public static int getRamsize()
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter RAM size : ");
        while(true)
        {
            if(input.hasNextInt())
            {
                Ramsize = input.nextInt();
                break;
            }
            else
            {
            input.nextLine();
            System.out.println("Invalid Input! Integer required");
            System.out.print("Please enter RAM size : ");
            }
        }
        return Ramsize;
    }
    public static int getfreespace()
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter HD free space : ");
        while(true)
        {
            if(input.hasNextInt())
            {
                freespace = input.nextInt();
                break;
            }
            else
            {
                input.nextLine();
                System.out.println("Invalid Input! Integer required");
                System.out.print("Please enter HD free space : ");

            }   
        }
        return freespace;
    }

    public static int getvideocard()
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter video card RAM size: ");
        while(true)
        {
            if(input.hasNextInt())
            {
                videocard = input.nextInt();
                break;
            }
            else
            {
                input.nextLine();
                System.out.println("Invalid Input! Integer required");
                System.out.print("Please enter video card RAM size: ");
            }   
        }
        return videocard;
    }

    public static void setuserID(String newuserID)
    {
        userID = newuserID;
    }
    public static void setpassword(String newpassword)
    {
        password = newpassword;
    }
    public static void setresolution(String newresolution) 
    {
        resolution = newresolution;
    }

    public static void setRamsize (int newRamsize)
    {
        Ramsize = newRamsize;
    }

    public static void setfreespace (int newfreespace)
    {
        freespace = newfreespace;
    }

    public static void setvideocard (int newvideocard)
    {
        videocard = newvideocard;
    }
    public static void main(String[] args)
    {
    setuserID(getuserID());
    setpassword(getpassword());
    setresolution(getresolution());
    setRamsize(getRamsize());
    setfreespace(getfreespace());
    setvideocard(getvideocard());
    System.out.println("You have input the following information: " + "\nuserID: " + userID 
            + "\npassword: " + password + "\nVideo resolution: " + resolution + "\nRam Size: " 
            + Ramsize + "\nHD Free Space: " + freespace + "\nVideo Card Ram Size: " + videocard);
    }
}

最佳答案

问题是因为您从未在有效的情况下执行任何操作,并且您使用 .next() 作为单个字符,而不是 .nextLine() ,后者会抓取行尾字符(返回字符)后输入的整个输入

这会一直询问,直到输入的内容满足您的 if 条件。

public static String getresolution()
{
    String resolution;
    boolean validAnswer = false;
    Scanner input = new Scanner(System.in);
    HashSet<String> validResolutions = new HashSet<>();
    validResolutions.add("800x600"); 
    validResolutions.add("1024x768"); 
    validResolutions.add("1152x900");
    //add more resolutions if you want without having to create a bigger if check 
    //validResolutions.add("1400x1120");

    do {
        System.out.print("Please enter video resolution: ");
        resolution = input.nextLine().replaceAll(" ", "").replaceAll("\n", "");
        validAnswer = validResolutions.contains(resolution) ? true : false;
        if(!validAnswer)
            System.out.println("Incorrect resolution please try again");
    } while (!validAnswer);

    return resolution;
}

关于java - 循环和封装的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41863397/

相关文章:

java - 复制文件时 JMeter Bean Shell Sampler 错误 "...Static method get( java.lang.String ) not found in class' java.nio.file.Paths”

java - Netbeans 中的 JTable : how do display table of many columns

java - 如何从 jar 文件中加载图像?

vb.net - 在另一个类中声明一个类的目的是什么?

java - 用Java模拟随机按键事件

java - Mahout的跨实体、多输入推荐

Android,我如何封装一个带有网络连接的 AsyncTask 类

oop - 依赖倒置原则 (SOLID) 与封装(OOP 的支柱)

javascript - 为什么在这个封装示例中私有(private)变量被赋值两次?

javascript - Javascript 中的封装/数据隐藏?