Java. Fundamentals. LoopsЦиклы. Циклы С предусловием Циклы С постусловием Со счетчиком.

Презентация:



Advertisements
Похожие презентации
Java Java java ISS, Wuhan University Nov., Java Java java Java Java Java ……
Advertisements

Object-Oriented Programming Ramzi Saifan Program Control Slides adapted from Steven Roehrig.
Синтаксис языка Java. Символы и синтаксис Перевод строчки эквивалентен пробелу Регистр в именах различается.
Arrays Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
English Version java.
Test 17 Вопрос 1. public class TKO { public static void main(String[] args) { String s = "-"; Integer x = 343; long L343 = 343L; if (x.equals(L343)) s.
Test 10 Вопрос 1. public class Test implements Iterator { // 1 private List list = new ArrayList (); // 2 public void addList(T... ts) { Collections.addAll(list,
b5_java_s4
Практическое использование Java Макаревич Л. Г.. Инсталляция Java Документация в docs Прописать PATH (каталог bin в JSDK) Прописать CLASSPATH (путь к.
Test 4 Вопрос 1. public class TestOutput { public static void main(String[] args) throws IOException { PrintStream out = new PrintStream( new BufferedOutputStream(
Test 9 Вопрос 1. public class A { private String runNow() { return "High"; } static class B extends A { public String runNow() { return "Low"; } } public.
Задача «Школа танцев» РОИ 2008 Автор задачи: Вадим Юрьевич Антонов Разбор: Сергей Игоревич Назаров.
Java Collections Framework (JCF) in Java Tutorial for students of universities Author: Oxana Dudnik.
Test 3 Вопрос 1. 01:package test; 02: public class Test { 03: public static void main(String [] args) { 04: Test test = new Test(); 05: System.out.println(test.toString());}
1. a=? b=? c=? {int a, b, c; a=(b=2+3)/2 - 4+(c=5%2); printf("%d %d %d \n", a, b, c); }
Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan Introduction and basics of Java Slides adapted from Steven Roehrig.
Test21 Вопрос 1. public class Test { void a1(Object... i){ System.out.println("[Object... i]"); } void a1(Integer... i){ System.out.println("[Integer...
Абстрактные типы данных 1. Абстрактная дата Date dt1, dt2; dt1 = new Date(1, Date.MARCH, 2006); dt2 = (Date)dt1.clone(); dt2.add(300); //
Функции с переменным числом аргументов private static int Sum(int a, int b) { return a + b; } static void Main() { int sum = Sum(1, 2); } 1 Функции.
1 A + B Операнд 1Операнд 2 Оператор Что такое выражение (expression) ? Что такое инструкция (statement) ? Операторы int max = (a > b) ? a : b;
Транксрипт:

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])); } } 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 } 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 { class DoWhile { public static void main(String args[]) { public static void main(String args[]) { int n = 5; int n = 5; do { do { System.out.println("Sample : " + n); System.out.println("Sample : " + n); n--; n--; }while(n > 0); }while(n > 0); } Output : Output : Sample : 5 Sample : 5 Sample : 4 Sample : 4 Sample : 3 Sample : 3 Sample : 2 Sample : 2 Sample : 1 Sample : 1 Sample : 0 Sample : 0

Цикл «for»(со счетчиком)

for(g = 0, h = 1; g < 6; ++g) for(g = 0, h = 1; g < 6; ++g) for(g = 0; g 1; ++g, h--) for(g = 0; g 1; ++g, h--) for(g = 5; g >= 1; --g) for(g = 5; g >= 1; --g) for(g = 0; g < 10; ++g, ++h, sum += g) for(g = 0; g < 10; ++g, ++h, sum += g) for(; x < 10; ++x) for(; x < 10; ++x) for(; ; ) 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[] nums = { 3, 1, 6, 4, 9, 5, 8, 2 }; int val = 5; int val = 5; boolean found = false; // ищем значение 5 в массиве boolean found = false; // ищем значение 5 в массиве for (int x : nums) for (int x : nums) { if (x == val) { if (x == val) { found = true; found = true; break; } break; } } if (found) { textInfo.setText("Значение найдено"); } if (found) { textInfo.setText("Значение найдено"); }

class prog_14 { class prog_14 { public static void main(String args[]){ public static void main(String args[]){ int numb[]={1,2,3,4,5}; int numb[]={1,2,3,4,5}; int summa=0; int summa=0; for(int i : numb){ for(int i : numb){ summa+=numb[i]; summa+=numb[i]; } System.out.println("Сумма="+summa); 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 } } } 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 } } }