重定向到if语句的开头 [英] Redirect to start of if statement

查看:77
本文介绍了重定向到if语句的开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import  java.util.Scanner; 

public class 工作{
public static void selectionMenu(){
System.out .println( 1.是奇数还是偶数。是完全数字\ nn3。查找GCD或LCM );
System.out.println( 4.查找palindrome \ nn5。查找总和数字\\\
6。退出);
System.out.print( 选择:);
}

public static void main( String [] args){

Scanner in = new Scanner(System.in);

int selection,newSelection;

System.out.println( \ t\t1.Math\ n \t\t2。几何);
System.out.print( 选择:);
selection = in.nextInt();

if (selection == 1 ){
selectionMenu( );
newSelection = in.nextInt();

if (newSelection == 1 ){
System。 out.print( 输入一个数字:);
int number = in.nextInt();
System.out.println( \t\t + EvenOrOdd(number ));

System.out.println();
selectionMenu();
newSelection = in.nextInt();
}

if (newSelection == 2 ){
System.out.print( 输入数字:);
int number = in.nextInt();
System.out.println( \t\t + isPerfect(number ));

System.out.println();
selectionMenu();
newSelection = in.nextInt();

}

if (newSelection == 3 ){
System.out.println( \ n1。查找GCD \ n2。查找LCM \ n3。退出);
System.out.print( 子选择:);
int subSelection = in.nextInt();

if (subSelection == 1 ){
System。 out.print( 分别输入两个数字:);
int number1 = in.nextInt();
int number2 = in.nextInt();
System.out.println( \t\tGCD( + number1 + + number2 + < span class =code-string>)=
+ gcd(number1,number2));
} else if (subSelection == 2 ){
System.out.print( 分别输入两个数字:);
int number1 = in.nextInt();
int number2 = in.nextInt();
System.out.println( \t\tLCM( + number1 + + number2 + < span class =code-string>)=
+ lcm(number1,number2));

}
}

}
}

public static 字符串 EvenOrOdd( int n) {
字符串 s = ;

if (n% 2 == 0 ){
s = n + 是偶数。;
} else {
s = n + 是一个奇数。;

}
return s;
}

public static 字符串 isPerfect( int n){
String s = < span class =code-string>
;
int sum = 0 ;

for int i = 1 ; i< n; i ++){
if (n%i == 0 ){
sum + = i;
}
}

if (sum == n){
s = n + 是一个完美的数字。;
} else {
s = n + 不是一个完美的数字。;
}

return s;
}

public static int gcd( int a, int b){
< span class =code-keyword> int
gcd = 1 ;
int k = 2 ;

while (k< = a&& k< = b){
if (a%k == 0 && b%k == 0
gcd = k;
k ++;
}

return gcd;
}

public static int lcm( int a, int b){
< span class =code-keyword> int x;
x =(a> b)? a:b;
while (true){
if (x%a == 0 && x%b == 0
返回 x;
++ x;
}

}

}



正如你可以看到我的程序,它运行正常。

当我运行它时,当我首先选择1(即数学)时,它会询问您可以看到的一系列选择。当我第一次进入2;在我完美地执行2之后,我要求另一个选择来执行另一个操作但是在我首先执行2并且选择1没有任何反应之后。

所以这里的问题现在是,(因为我们都知道java逐行工作)所以如何让它在执行选择之后在if语句的开头处进行,以便我能够在我刚刚执行的那个之前检测其他选择?



我尝试了什么:



我试图在每个结尾处输入一个新的选择如果选择声明,但都没有用。

解决方案

我把它放在这样的循环中......



  while (选择!=  3 
{
System.out.println( \ t\t1.Math\ n\t\t2.Geometry \ n \\\\\\\\\\\\\\\\\\\\\\\
System.out.print( 选择:);
selection = in.nextInt();

if (selection == 1 ){
selectionMenu( ); .....


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



看起来你需要学习课程循环,见 while 循环

看起来你已经知道 lo操作,你只需要在循环内重复代码。



你知道吗 lcm(a,b)= a * b / gcd (a,b)



你的 gcd 效率不高。如果 a%b == 0 ,则 gcd = b ,否则 gcd(a,b)= gcd(b,a%b)

import java.util.Scanner;

public class Work {
	public static void selectionMenu() {
		System.out.println("1. Is Odd or Even\n2. Is Perfect number\n3. Find GCD or LCM");
		System.out.println("4. Find palindrome\n5. Find sum digits\n6. Exit");
		System.out.print("Selection: ");
	}

	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);

		int selection, newSelection;

		System.out.println("\t\t1. Math\n\t\t2. Geometry");
		System.out.print("Selection: ");
		selection = in.nextInt();

		if (selection == 1) {
			selectionMenu();
			newSelection = in.nextInt();

			if (newSelection == 1) {
				System.out.print("Enter a number: ");
				int number = in.nextInt();
				System.out.println("\t\t" + EvenOrOdd(number));

				System.out.println();
				selectionMenu();
				newSelection = in.nextInt();
			}

			if (newSelection == 2) {
				System.out.print("Enter a number: ");
				int number = in.nextInt();
				System.out.println("\t\t" + isPerfect(number));

				System.out.println();
				selectionMenu();
				newSelection = in.nextInt();

			}

			if (newSelection == 3) {
				System.out.println("\n1. Find GCD\n2. Find LCM\n3. Exit");
				System.out.print("Sub selection: ");
				int subSelection = in.nextInt();

				if (subSelection == 1) {
					System.out.print("Enter two numbers respectively: ");
					int number1 = in.nextInt();
					int number2 = in.nextInt();
					System.out.println("\t\tGCD (" + number1 + "," + number2 + ") = " + gcd(number1, number2));
				} else if (subSelection == 2) {
					System.out.print("Enter two numbers respectively: ");
					int number1 = in.nextInt();
					int number2 = in.nextInt();
					System.out.println("\t\tLCM (" + number1 + "," + number2 + ") = " + lcm(number1, number2));

				}
			}

		}
	}

	public static String EvenOrOdd(int n) {
		String s = "";

		if (n % 2 == 0) {
			s = n + " is an Even number.";
		} else {
			s = n + " is an Odd number.";

		}
		return s;
	}

	public static String isPerfect(int n) {
		String s = "";
		int sum = 0;

		for (int i = 1; i < n; i++) {
			if (n % i == 0) {
				sum += i;
			}
		}

		if (sum == n) {
			s = n + " is a perfect number.";
		} else {
			s = n + " is not a perfect number.";
		}

		return s;
	}

	public static int gcd(int a, int b) {
		int gcd = 1;
		int k = 2;

		while (k <= a && k <= b) {
			if (a % k == 0 && b % k == 0)
				gcd = k;
			k++;
		}

		return gcd;
	}

	public static int lcm(int a, int b) {
		int x;
		x = (a > b) ? a : b;
		while (true) {
			if (x % a == 0 && x % b == 0)
				return x;
			++x;
		}

	}

}


As you can see my program, it works fine.
When i run it, when i select 1 at first (that is math) it asks for series of choices as you can see. When i first enter 2; after i execute 2 perfectly i made it to ask for another choice to carry out another operation but after i execute 2 at first and select 1 nothing happens.
So the question here is now, (as we all know java works line by line) so how do i make it to go at the start of the if statement after executing a selection so that it can me able to detect the other selections before the one i just executed?

What I have tried:

I tried to enter a new selection at the end of each if selection statement but all to no avail.

解决方案

I'd put it in a while loop something like this...

while(selection != 3)
{
    System.out.println("\t\t1. Math\n\t\t2. Geometry\n\t\t3. Exit");
    System.out.print("Selection: ");
    selection = in.nextInt();
 
    if (selection == 1) {
	selectionMenu(); .....


You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Looks like you need to learn the lesson loops, see "while" loops
Looks like you already know while loops, you just have to put the code to repeat inside the loop.

Do you know that lcm(a, b)= a * b / gcd(a, b)

Your gcd is not efficient. If a % b == 0, then gcd= b, otherwise gcd(a, b)= gcd(b, a % b).


这篇关于重定向到if语句的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆