用java 编写一个程序,要求要有一个自己定义的循环和两个函数,要够50行左右
2个回答
展开全部
新鲜出炉,刚写的:
import java.util.ArrayList;
import java.util.Scanner;
public class LoopAndFunction {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number;
ArrayList<Integer> theList = new ArrayList<Integer>();
System.out.print("Enter a number for list, end with -1: ");
number = input.nextInt();
while(number != -1){
theList.add(number);
System.out.print("Enter a number for list, end with -1: ");
number = input.nextInt();
}
System.out.println("the list is: " + theList.toString());
sortArray(theList);
System.out.println("after sorting, the list is: " + theList.toString());
for(int i = 0; i < theList.size(); i++){
int eachNumber = theList.get(i);
if(isOddNumber(eachNumber)){
System.out.println(eachNumber + " is odd number");
}
else{
System.out.println(eachNumber + " is even number");
}
}
}
public static boolean isOddNumber(int number){
boolean isOdd = false;
if(number % 2 == 1){
isOdd = true;
}
return isOdd;
}
public static void sortArray(ArrayList<Integer> list){
for(int i = 0; i < list.size() - 1; i++){
int temp = list.get(i);
for(int j = i + 1; j < list.size(); j++){
if(list.get(i) > list.get(j)){
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询