Circles in Java.For students
Java. Fundamentals. LoopsЦиклы С предусловием Циклы С постусловием Со счетчиком Циклы Цикл с предусловием while public class SumNumbers { int n=100; public SumNumbers(int n) { this.n = n; int i=1, sum=0, p=1; while(i<=this.n){ sum+=i; p*=i; System.out.println("Summa of numbers="+sum); System.out.println("Proizvedenie of numbers=" + p); i++; } System.out.println("Summa of numbers="+sum); System.out.println("Proizvedenie of numbers=" + p); }public static void main(String[] args){ int n=args.length; if(n==0)return; SumNumbers sumNumbers=new SumNumbers(Integer.parseInt(args[0]));}} Цикл с постусловием Цикл “do-while” public class SumDigitsOfNumber { public static void main(String args[]){ int a, a1, n=124, s=0; do{ a=n % 10; a1=n / 10; System.out.println("a="+a); s=s+a; System.out.println("s="+s); n=n/10; } while(n>0); System.out.print("Sum = "+s); }//end of main} Printing Numbers class DoWhile {
public static void main(String args[]) {
int n = 5;
do {
System.out.println("Sample : " + n);
n--;
}while(n > 0);
}
Output :
Sample : 5
Sample : 4
Sample : 3
Sample : 2
Sample : 1
Sample : 0 Цикл «for»(со счетчиком) for(g = 0, h = 1; g < 6; ++g) for(g = 0; g < 3 && h > 1; ++g, h--) for(g = 5; g >= 1; --g) for(g = 0; g < 10; ++g, ++h, sum += g) for(; x < 10; ++x) for(; ; ) “for-each” for(int i: myArray){}for (тип итерационная_переменная: коллекция) { блок операторов;} for(type var : collection) {statement-block } int[] nums = { 3, 1, 6, 4, 9, 5, 8, 2 }; int val = 5; boolean found = false; // ищем значение 5 в массиве for (int x : nums) { if (x == val) { found = true; break; } } if (found) { textInfo.setText("Значение найдено"); } class prog_14 {public static void main(String args[]){int numb[]={1,2,3,4,5};int summa=0;for(int i : numb){summa+=numb[i];}System.out.println("Сумма="+summa);}} return import javax.swing.*;import java.awt.*;import java.util.Random;public class RandomCircles extends JPanel{ String ans; int count; Color randomColor; int R,G, B; public RandomCircles() { ans = JOptionPane.showInputDialog("Enter the number of circles"); count= Integer.valueOf(ans); int i=0; repaint(); } public void paintComponent(Graphics page) // public void paint(Graphics page) { Random generator = new Random(); int x, y, diameter; for(int i = 0; i < count; i++) { //loop that takes the count and does this "x" times R = (int) (Math.random( )*256); G = (int) (Math.random( )*256); B= (int)(Math.random( )*256); randomColor = new Color(R, G, B); page.setColor(randomColor);//sets color to blue x = generator.nextInt(90);//random location for x y = generator.nextInt(90);//random location for y diameter = generator.nextInt(30);//random size page.fillOval(x, y, diameter, diameter);//draws the circle } }}