Skip to content

Instantly share code, notes, and snippets.

@Terng
Last active March 8, 2019 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Terng/666920ffe6a8339e40144187e7ece8df to your computer and use it in GitHub Desktop.
Save Terng/666920ffe6a8339e40144187e7ece8df to your computer and use it in GitHub Desktop.
lab 3 (loop แสดงตัว & และเว้นวรรค กับ รูปแสดงแถว)
package com.terng;
public class Main {
public static void main(String[] args) {
for (int i=0; i<=3;i++)
{
System.out.println();
for (int j=0;j<=9;j++)
{
System.out.print("&");
}
if (i==2)
{
System.out.print("\n");
}
}
}
}
/////////////////////////////////////
import java.util.Scanner;
public class Loop2
{
public static void main(String[] args)
{
System.out.print("Please input the num for loop : ");
Scanner n = new Scanner(System.in);
int num = n.nextInt();
System.out.print("input : " +num+"\n");
int counter = num;
for (int i = 1; i<=num; i++ )
{
System.out.println();
for (int j = counter; j >=1;j--)
{
System.out.print(j + " ");
}
counter--;
}
}
}
package com.terng;
import java.util.Scanner;
public class Main {
public static void main(String args[]){
System.out.print("Please input the num for loop : ");
Scanner n = new Scanner(System.in);
int num = n.nextInt();
System.out.print("input : " +num+"\n");
int counter = num;
for(int i=counter; i>0 ;i--){
for(int j=0; j < i; j++){
System.out.print(j+1 +" ");
}
System.out.println("");
}
}
}
package com.terng;
import java.util.Scanner;
public class Main {
public static void main(String args[]){
System.out.print("Please input the num for loop : ");
Scanner n = new Scanner(System.in);
int num = n.nextInt();
System.out.print("input : " +num+"\n");
int counter = num;
for(int i=counter; i>=1 ;i--){
for(int j=i; j <= counter; j++){
System.out.print(j +" ");
}
System.out.println("");
}
}
}
package com.terng;
import java.util.Scanner;
public class Main {
public static void main(String args[]){
System.out.print("Please input the num for loop : ");
Scanner n = new Scanner(System.in);
int num = n.nextInt();
System.out.print("input : " +num+"\n");
int counter = num;
for(int i=1; i<=counter ;i++){
for(int j=i; j >= 1; j--){
System.out.print(j +" ");
}
System.out.println("");
}
}
}
package com.terng;
import java.util.Scanner;
public class Main {
public static void main(String args[]){
System.out.print("Please input the num for loop : ");
Scanner n = new Scanner(System.in);
int num = n.nextInt();
System.out.print("input : " +num+"\n");
int counter = num;
for(int i=1; i<=counter ;i++){
for(int j=1; j <= i; j++){
System.out.print(i +" ");
}
System.out.println();
}
}
}
@Terng
Copy link
Author

Terng commented Oct 13, 2017

ผลลัพธ์อันที่1
image

ผลลัพธ์อันที่ 2
image

@Terng
Copy link
Author

Terng commented Oct 14, 2017

lab 3 2.2 output
image

@Terng
Copy link
Author

Terng commented Oct 14, 2017

lab 3 2.3 output
image

@Terng
Copy link
Author

Terng commented Oct 14, 2017

lab 3 2.4 output
image

@Terng
Copy link
Author

Terng commented Oct 14, 2017

เพิ่มเติมลองเข้าไปดูเว็บนี้
https://codeography.wordpress.com/2016/08/06/1-21-321-4321-54321/
แต่เป็นภาษา C ให้ดู for อย่างเดียวแล้วเอามาเปลี่ยน

@Terng
Copy link
Author

Terng commented Oct 14, 2017

lab 3 2.5 output
image

@Terng
Copy link
Author

Terng commented Mar 8, 2019

public class Main {

    public static void main (String[]args)
    {
        for(int r=5;r>=1;r--)
        {
            for(int c=1;c<=r;c++)
            {
                System.out.print(r);
            }
            System.out.println();
        }
    }

}
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment