F6720  

1.試寫一程式,可計算出1到100間所有3的倍數之總和。

import java.io.*;

public class Ex6_ex1_01
{
public static void main(String[] args) throws IOException
{
// 宣告累加值sum及計算範圍range
int sum = 0, range = 100;
System.out.println("1~100間所有3偣數總和:");

int i = 0; // 宣告迴圈變數i
while (i < range)
{ // 當i值大於range即停止執行的while迴圈
sum += i; // 每次進入迴圈時,將sum的值加上i
i += 3; // 每次是將i值加2
}
System.out.println("1~100" + "的所有3倍數總和為" + sum);
}
}

 

2.試寫一程式,讓使用者輸入任意正整數N,並利用for迴圈在螢幕上輸出1*1、2*2、...、N*N之結果。

 

import java.io.*;

 

public class Ex6_ex2
{
public static void main(String[] args) throws IOException
{
java.util.Scanner scanner = new java.util.Scanner(System.in);

int x;
System.out.print("請輸入要乘的數:");
int n = scanner.nextInt();
for (x = 1; x < n + 1; x++)
{
System.out.printf(x + "*" + x + "=" + x * x + "\n"); // 輸出y*x=結果
}
}
}

 

3.試寫一程式,讓使用者輸入兩個整數,並計算兩整數間所有整數的和。

import java.util.Scanner;

 

public class Ex6_ex3_1
{
public static void main(String[] args)
{
while (true)
{
Scanner sc = new Scanner(System.in);
System.out.print("請輸入要的數one(輸入0就會結束):");
int a = sc.nextInt();
if (a == 0)
{
break;
} // 若使用者輸入0,就跳出迴圈
System.out.print("請輸入要的數two(輸入0就會結束):");
int b = sc.nextInt();
int c = 0;
if (a > b)
{
int temp;
temp = a;
a = b;
b = temp;
}
for (int i = a; i <= b; i++)
{
c = c + i; // 累加的程式

}
System.out.println("加總結果為:" + c);
}
}
}

 4.承上題,請將程式加上是否繼續運算的選項(例如只要輸入'0'即結束),並將總和不斷累加。

import java.util.Scanner;


public class Ex6_ex4 {
public static void main(String[] args)
{
int c = 0,c2,choose,temp,i,a,b; //先宣告整數值
Scanner sc = new Scanner(System.in); //使用者輸入的程式

while(true) // 讓使用者可反覆輸入新數值的迴圈
{
c2=0; //不累加數字,每一次作累加運算前都重設回0
System.out.printf("請問您要持續累加數字嗎? (1)要, (2)不要, (0)結束程式:");
choose=sc.nextInt();//使用者輸入的程式
if(choose==0)
{
System.exit(0); //正常結束的程式碼
}

System.out.print("請輸入起始數字one(輸入0就會結束):");
a=sc.nextInt();
if(a==0)
{
break; //若使用者輸入0, 就跳出迴圈
}

System.out.print("請輸入結束數字two(輸入0就會結束):");
b=sc.nextInt();
if(b==0)
{
break; //若使用者輸入0, 就跳出迴圈
}
if(a>b)
{
temp=a;
a=b;
b=temp;
}
if(choose == 1)
{
for(i=a; i<=b; i=i+1)
{
c = c +i ; //累加的程式
}
System.out.println("加總結果為:"+c);
}
}
}
}

 

5.試寫一程式,可從螢幕輸出1到100之間屬於5或7的倍數的數值。

import java.io.IOException;

public class Ex6_ex5
{
public static void main(String[] args) throws IOException
{
System.out.print("5的倍數為 :");
for (int i = 1; i <= 100; i++)
{
if (i % 5 == 0)
{
System.out.print(i+" ");
}
}
System.out.println("");
System.out.print("7的倍數為 :");
for (int i = 1; i <= 100; i++)
{
if (i % 7 == 0)
{
System.out.print(i+" ");
}
}
}
}

------------------另一種輸入法-------------------

import java.io.*;

public class Ex6_ex5_1
{
public static void main(String[] args)
{
System.out.println("此程式為找出7或5的倍數在100與1之間 : ");
int i;
System.out.print("五的倍數為 :");
for (i = 1; i <= 100; i++)
{
if (i % 5 == 0)// 如果i除於5餘數為0
{
System.out.print(i + ",");
}
}
System.out.println();
System.out.print("七的倍數為 :");
int a;
for (a = 1; a <= 100; a++)
{
if (a % 7 == 0)
{
System.out.print(a + ",");
}
}
}
}

 6.試寫一個程式,可讓使用者輸入矩形的長寬,並於螢幕輸出為星號*所組成的矩形,例如輸入5和3時會輸出:

@@@@@
@@@@@
@@@@@

 

 

public class Ex6_ex6
{
public static void main(String[] args)
{
java.util.Scanner scanner = new java.util.Scanner(System.in);
System.out.print("輸入高:");
int l = scanner.nextInt();
System.out.print("輸入長:");
int w = scanner.nextInt();

for (int i = l; i > 0; i--)
{
for (int j = w; j > 0; j--)
{
System.out.print("@");
} // 內層for
System.out.println();
} // 外層for
}
}

 7.試寫一程式,可以繪製出如下圖的菱形:

    * 
  * * * 
  * * * * * 
* * * * * * * 
  * * * * * 
  * * * 
    * 

 

public static void main(String[] args)
{
java.util.Scanner scanner = new java.util.Scanner(System.in);
System.out.print("輸入數字: ");
int N = scanner.nextInt();
for (int i = 1; i < N + 1; i++)
{
for (int m = N - i; m > 0; m--)
{
System.out.print(" ");
}
for (int j = i * 2 - 1; j > 0; j--)
{
System.out.print(" * ");
scanner.close();
}
System.out.println();
}
for (int k = N - 1; k > 0; k--)
{
for (int n = N - k; n > 0; n--)
{
System.out.print(" ");
}
for (int l = k * 2 - 1; l > 0; l--)
{
System.out.print(" * ");
scanner.close();
}
System.out.println();
}
}
}

 

arrow
arrow

    Lineage 發表在 痞客邦 留言(0) 人氣()