有没有办法在Java中从Case 1获取输入并传递到Case 2?

huangapple go评论65阅读模式
英文:

is there a way to take input from case 1 to case 2 in java?

问题

case 1 用户需要验证XML文件
case 2 我需要检查用户是否验证了XML文件如果验证通过 = true我需要显示XML文件的信息
我的问题是 **我如何检查用户是否验证了XML**

即使我验证了XML当我插入2时它总是为false

    boolean valid = false;

      switch (number) {
          case 1:
              validation();
              break;

          case 2:
              if (validtrue(valid) == true)
                  System.out.println("true");// 检查中
              else
                  System.out.println("请先验证XML文件");
              break;

    static void validation() {

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入带有 .xml 后缀的XML文件名");
        String xmlFName = sc.nextLine();
        try {

            File file = new File(xmlFName); // 验证xml
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(file);
            NodeList lst = doc.getElementsByTagName("book");
        } catch (Exception e) {

            System.out.print(e.getMessage());
        }

        System.out.println(xmlFName + " 文件已通过验证");
        boolean valid = true;
        validtrue(valid);
    }

    static boolean validtrue(boolean valid) {

        if (valid == true)
            return true;
        else
            return false;
    }

注意:为了保持代码的一致性,我将保留了部分代码中的英文术语(如变量名和方法名)。

英文:

case 1 the user needs to validate the XML file.
case 2 I need to check if the user validated the XML file and if validation = true I need to display the info of the XML file
my problem is how can I check if the user validated the XML

even if I validate the XML when I inset 2 it is always false

boolean valid = false;
switch (number)
{
case 1 :
validation();
break;
case 2 :
if (validtrue(valid) == true) 
System.out.println("true");// checking 
else
System.out.println("please validate the XML file first  ");
break;
static void validation()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the name of te XML file wiht .xml");
String xmlFName = sc.nextLine();
try {
File file = new File(xmlFName); // validate the xml 
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(file);
NodeList lst = doc.getElementsByTagName("book");
}catch(Exception e)
{
System.out.print(e.getMessage());
}
System.out.println("the "+ xmlFName +" file has been validated ");
boolean valid = true;
validtrue(valid);
}
static boolean validtrue(boolean valid)
{
if valid == true 
return true;
else return false;
}

答案1

得分: 0

如果我理解正确,您有两个菜单选项:1 其中用户验证某事,2 其中用户在进行验证后执行某些操作。并且要求用户能够在不执行选项 1 的情况下转到选项 2。您可以尝试类似以下的代码:

public static void main(String[] args) {
    Scanner myInput = new Scanner(System.in);
    boolean validated = false;
    int option;
    while (true) {
        option = myInput.nextInt();

        switch (option) {
            case 1:
                validated = validation();
                break;
            case 2:
                if (validated) {
                    System.out.println("true");// 检查
                } else {
                    System.out.println("请先验证 XML 文件");
                }
                break;
        }
    }
}

private static boolean validation() {
    return true; // 或者 false
}
英文:

If I understand correctly you have two menu options: 1 where user validates something and 2 where user does something if validation has been made. And it is required that user should be able to go to option 2 without doing option 1. You could try something like this

public static void main(String[] args) {
Scanner myInput = new Scanner( System.in );
boolean validated = false;
int option;
while(true){
option = myInput.nextInt();
switch (option) {
case 1:
validated = validation();
break;
case 2:
if (validated){
System.out.println("true");// checking
}
else{
System.out.println("please validate the XML file first  ");
}
break;
}
}
}
private static boolean validation(){
return true; //or false
}

huangapple
  • 本文由 发表于 2020年10月13日 14:34:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64329812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定