2016a
Материал из PTHSWiki
Версия от 09:17, 24 сентября 2013; Antonk (обсуждение | вклад)
Содержание |
Задание по отладке
public class Errors {
public static void main(String[] args) {
int arr[] = new int[20];
for (int i = 0; i <= 20; i++) {
arr[i] = i*i;
}
int sum = 0;
for (int i = 0; i <= 20; i++) {
if (i % 2 == 0) {
sum = sum + arr[i];
} else {
sum = sum - arr[i];
}
}
System.out.println(sum);
}
}
Чтение массива из файла (BufferedReader - по одному числу в строке)
/*
* Читает из файла input.txt
* В первой строке число n - количество чисел.
* В следующих n строках по одному числу в строке
*
* Пример:
* 6
* 12
* 1
* 41
* 2
* 54
* 13
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
int n = Integer.parseInt(br.readLine());
int arr[] = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
Чтение массива из файла (Scanner - можно числа в одной строке)
/*
* Читает из файла input.txt
* В первой строке число n - количество чисел.
* Во второй - n чисел.
*
* Пример:
* 6
* 12 1 41 2 54 13
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner sc = new Scanner(new File("input.txt"));
int n = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
А+Б. Ввод из файла. Вывод в файл
import java.io.*;
import java.util.*;
public class aplusb {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new File("aplusb.in"));
PrintWriter out = new PrintWriter(new File("aplusb.out"));
int a = in.nextInt();
int b = in.nextInt();
out.println(a + b);
in.close();
out.close();
}
}
Ввод двух чисел в строку
Scanner sc=new Scanner(System.in); int num1=sc.nextInt(); int num2=sc.nextInt();
Ввод с клавиатуры
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
int a, b;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
a = Integer.parseInt(br.readLine());
b = Integer.parseInt(br.readLine());
System.out.println(a+b);
}
}
Функции
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
int a, b, c;
double d;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
a = Integer.parseInt(br.readLine());
b = Integer.parseInt(br.readLine());
c = sum(a, b);
d = mySqrt(b);
System.out.println(c);
System.out.println(d);
}
public static double mySqrt(int a) {
return Math.sqrt(a);
}
public static int sum(int a, int b) {
System.out.println("I really want to sleep");
return a+b;
}
public static int max2(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
}
Создание собственных классов
import java.util.Scanner;
public class Main {
public static class Person {
public String name;
public int ege;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Person arr[] = new Person[2];
for (int i = 0; i < arr.length; i++) {
arr[i] = new Person();
arr[i].name = sc.next();
arr[i].ege = sc.nextInt();
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i].ege + " " + arr[i].name);
}
}
}