分别使用3种方法找出1——100之间,既能被4整除,也能被6整除的所有数,并输出。z最好用do...while和while做。
谢谢各位了!
分别使用3种方法找出1——100之间,既能被4整除,也能被6整除的所有数,并输出。z最好用do...while和while做。
谢谢各位了!
package com.newer.test;
import java.util.Scanner;
public class Test {
private int index;
private int last;
public Test(){}
public Test(int index,int last){
int temp;
if(index>last){
temp = index;
index = last;
last = temp;
}
this.index = index;
this.last = last;
}
public void getFor(){
for(int i = index; i<=last; i++){
if( i%4==0 && i%6==0){
System.out.print( i + "\t" );
}
}
System.out.println();
}
public void getWhile(){
int i = this.index;
while( i <=this.last ){
i ++;
if( i%4==0 && i%6==0){
System.out.print( i + "\t" );
}
}
System.out.println();
}
public void getDoWhile(){
int i = this.index;
do{
i ++;
if( i%4==0 && i%6==0){
System.out.print( i + "\t" );
}
}while( i <= this.last );
System.out.println();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//分别使用3种方法找出1——100之间,
//既能被4整除,也能被6整除的所有数,并输出。
//z最好用do...while和while做。
try {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:");
int index = sc.nextInt();
System.out.println("请输入第二个数:");
int last = sc.nextInt();
System.out.println("既能被4整除,也能被6整除的所有数如下所示:");
Test test = new Test(index,last);
//方法一:for
System.out.println("Method1(for):");
test.getFor();
//方法二:while
System.out.println("Method2(while):");
test.getWhile();
//方法三:do..while
System.out.println("Method3(do..while):");
test.getDoWhile();
} catch (Exception e) {
//e.printStackTrace();
System.out.println("输入格式错误!");
}
}
}
希望对你有帮助!
额
鄙人来咯
思路是这样的 变量 int i = 4;
先用循环 从3到100内找出来
if判断 在循环里面i自加1 i++;
i%4==0
System.out.println(i);
// 变量 i 要等于四哦 因为一个好的程序员是不会浪费掉一次循环的
//
public class He{
public static void main(String[] args){
int i = 3;
do{
if(i%4==0){
System.out.println(i);
}
i++;
}while(i<=100);
}
}
(1)while
int i = 1; while (i<=100){ if(i % 4 == 0 && i % 6 == 0){ System.out.print(i);
System.out.print(); i++; } }
(2)do...while
do{
if(i % 4 == 0 && i % 6 == 0){ System.out.print(i);
System.out.print(); i++; } while (i<=100)
}
第一种:
public class Test {
public static void main(String[] args) { Test t = new Test(); t.find(); } //输出既能被4整除又能被6整除的1---100的数 private void find() { int i = 1;
while(i<=100) { boolean f = divfour(i); boolean s = divsix(i); if(f&&s) { System.out.println(i); } i++; } } //判断是否能被4整除 private boolean divfour(int num) { boolean is = false; if(num%4==0) { is = true; } return is; } //判断是否能被6整除 private boolean divsix(int num) { boolean is = false; if(num%6==0) { is = true; } return is; }
}
第二种:do-while
public class Test {
public static void main(String[] args) { Test t = new Test(); t.find(); } //输出既能被4整除又能被6整除的1---100的数 private void find() { int i = 1; do { boolean f = divfour(i); boolean s = divsix(i); if(f&&s) { System.out.println(i); } i++; }while(i<=100); } //判断是否能被4整除 private boolean divfour(int num) { boolean is = false; if(num%4==0) { is = true; } return is; } //判断是否能被6整除 private boolean divsix(int num) { boolean is = false; if(num%6==0) { is = true; } return is; } } 第三种:
public class Test {
public static void main(String[] args) { Test t = new Test(); t.find(); } //输出既能被4整除又能被6整除的1---100的数 private void find() {
for(int i=1;i<=100;i++) { boolean f = divfour(i); boolean s = divsix(i); if(f&&s) { System.out.println(i); } }
} //判断是否能被4整除 private boolean divfour(int num) { boolean is = false; if(num%4==0) { is = true; } return is; } //判断是否能被6整除 private boolean divsix(int num) { boolean is = false; if(num%6==0) { is = true; } return is; }
}