这个程序有什么问题?循环只执行一次? [英] What is wrong with this program? Loop is executing only once?

查看:55
本文介绍了这个程序有什么问题?循环只执行一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 / *。 Evens数是一个整数,其数字都是偶数。例如,2426是Evens数字,但3224不是。 

编写一个名为isEvens的函数,如果其整数参数为

则返回1一个Evens数,否则返回0.

函数签名是
int isEvens(int n)
* /
import java.util.Scanner;
公共类Evenoroddnumber {

int status,n,k,evencount = 0,oddcount = 0,temp = 0;

int isEvens(int n)
{System.out.println(从main输入的数字是+ n);
if(n< 0)
{
System.out.println(number is negative);
返回0;
}
else if(n> 0)
{System.out.println(number is positive);
temp = n%10;
System.out.println(提醒是+临时);
if(temp%2 == 0)
{
System.out.println(提醒是偶数+临时);
evencount ++;
System.out.println(count even number+ evencount);
n = n / 10;
System.out.println(数字除以10是+ n);
}
其他
{
System.out.println(提醒是奇数+临时);
oddcount ++;
}
}
if(oddcount!= 0)
{
return 0;
}
其他
{
返回1;
}
}

public static void main(String args [])
{
Scanner s = new Scanner(System.in);
int n = s.nextInt();
Evenoroddnumber e1 = new Evenoroddnumber();
int x = e1.isEvens(n);


}

}





我是什么尝试过:



 / *。 Evens数是一个整数,其数字都是偶数。例如,2426是Evens数字,但3224不是。 

编写一个名为isEvens的函数,如果其整数参数为

则返回1一个Evens数,否则返回0.

函数签名是
int isEvens(int n)
* /
import java.util.Scanner;
公共类Evenoroddnumber {

int status,n,k,evencount = 0,oddcount = 0,temp = 0;

int isEvens(int n)
{System.out.println(从main输入的数字是+ n);
if(n< 0)
{
System.out.println(number is negative);
返回0;
}
else if(n> 0)
{System.out.println(number is positive);
temp = n%10;
System.out.println(提醒是+临时);
if(temp%2 == 0)
{
System.out.println(提醒是偶数+临时);
evencount ++;
System.out.println(count even number+ evencount);
n = n / 10;
System.out.println(数字除以10是+ n);
}
其他
{
System.out.println(提醒是奇数+临时);
oddcount ++;
}
}
if(oddcount!= 0)
{
return 0;
}
其他
{
返回1;
}
}

public static void main(String args [])
{
Scanner s = new Scanner(System.in);
int n = s.nextInt();
Evenoroddnumber e1 = new Evenoroddnumber();
int x = e1.isEvens(n);


}

}

解决方案

你说的是循环只执行一次,但你没有循环。

在你的main方法中,尝试在你读取下一个数字的语句周围循环并测试它是否是偶数。当你输入用完时,循环应该结束。


正如BillW33所说:你没有循环。

你需要在你的内部 Evenoroddnumber 函数以及 main - main 循环允许你处理多个数字,你需要的那个 Evenoroddnumber 需要在那里处理用户输入中的每一个数字。

基本上,你需要说:

1)虽然数字> 0

1.1)将最低有效数字输入一个名为数字的变量

1.2)如果数字是奇数,返回

1.3)否则,将数字除以10并再次循环

2)返回 true


要构建算法,最好的办法是先在纸上写下来;也就是说,在尝试制作电脑之前先手动完成。



你的任务的伪代码可以是

<如果n为奇数,则返回0
除以n
$ b返回1



您无需计算偶数的数量。你也不需要所有这些全局变量。

现在尝试将伪代码转换为java,使用你的课程并询问你的老师是否有你不理解的东西。

/*. An Evens number is an integer whose digits are all even. For example 2426 is an Evens number but 3224 is not.

Write a function named isEvens that returns 1 if its integer argument is 

an Evens number otherwise it returns 0.

The function signature is
   int isEvens (int n)
*/
import java.util.Scanner;
public class Evenoroddnumber {
	
	int status,n,k,evencount=0,oddcount=0,temp=0;
	
	int isEvens (int n)
	{System.out.println("number entered from main is"+n);
		if(n<0)
		{
			System.out.println("number is negative");
			return 0;
		}
		else if(n>0)
		{System.out.println("number is positive");
			temp=n%10;
			System.out.println("reminder is"+temp);
			if(temp%2==0)
			{
				System.out.println("reminder is even"+temp);
				evencount++;
				System.out.println("count even number"+evencount);
				n=n/10;
				System.out.println("number devided by 10 is"+n);
			}
			else 
			{
				System.out.println("reminder is odd"+temp);	
				oddcount++;
			}
		}
		if(oddcount!=0)
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}

	public static void main(String args[])
	{
		Scanner s=new Scanner(System.in);
		int n=s.nextInt();		
		Evenoroddnumber e1=	new Evenoroddnumber();
		int x=e1.isEvens(n);
		
		
	}

}



What I have tried:

/*. An Evens number is an integer whose digits are all even. For example 2426 is an Evens number but 3224 is not.

Write a function named isEvens that returns 1 if its integer argument is 

an Evens number otherwise it returns 0.

The function signature is
   int isEvens (int n)
*/
import java.util.Scanner;
public class Evenoroddnumber {
	
	int status,n,k,evencount=0,oddcount=0,temp=0;
	
	int isEvens (int n)
	{System.out.println("number entered from main is"+n);
		if(n<0)
		{
			System.out.println("number is negative");
			return 0;
		}
		else if(n>0)
		{System.out.println("number is positive");
			temp=n%10;
			System.out.println("reminder is"+temp);
			if(temp%2==0)
			{
				System.out.println("reminder is even"+temp);
				evencount++;
				System.out.println("count even number"+evencount);
				n=n/10;
				System.out.println("number devided by 10 is"+n);
			}
			else 
			{
				System.out.println("reminder is odd"+temp);	
				oddcount++;
			}
		}
		if(oddcount!=0)
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}

	public static void main(String args[])
	{
		Scanner s=new Scanner(System.in);
		int n=s.nextInt();		
		Evenoroddnumber e1=	new Evenoroddnumber();
		int x=e1.isEvens(n);
		
		
	}

}

解决方案

You say that the loop is executing only once, but you don't have a loop.
In your main method try putting a loop around the statements where you read the next number and test if it is even. The loop should end when you run out of input.


As BillW33 has said: you don't have a loop.
And you need on inside your Evenoroddnumber function as well as in your main - the main loop allows you to process multiple numbers, and the one you need inside Evenoroddnumber needs to be there so as to process every digit in the user input.
Basically, you need to say:
1) While number > 0
1.1) Get least significant digit into a variable called digit
1.2) If digit is odd, return false.
1.3) Otherwise, divide number by ten and go round the loop again
2) Return true


To construct an algorithm, best way is write it down on a paper first; that is, do it by hand before trying to make a computer do it.

A pseudo-code for your task could be

while n is greater than zero
   if n is odd, return 0
   divide n by 10
return 1


You do not need to count the number of even numbers. Neither do you need all these global variables.
Now try to translate that pseudo code to java, using your course and asking your teacher if there is something you do not understand.


这篇关于这个程序有什么问题?循环只执行一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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