Unit  1:: Programming with Java

✔️

机械、自动、不需要智慧地执行原始的内置指令。

字节码相同,JVM不同(体现平台)

✖️

In modern computers instructions can be stored and manipulated as other types of data. 对,都是位运算

We cannot describe an algorithm without a programming language.错,没有语言也可以写算法

compile:高级语言变成低级语言,设置一次就行,只翻译 不执行 很快,形成字节码。interpreter:要设置很多次,又翻译又执行。

z = x * y, 改y, z不变

用byte(带符号整数)存100。Although such data values can be stored in bigger numeric data types, e.g. short, int, or long, the most appropriate is byte as it takes least memory and has the range from -128 to 127.

double可以是也可以不是浮点型小数

打印前100个prime numbers

第一个x设为2,然后逐个往后试验

public static void main(String[] args)
{
Scanner scan = new Scanner(System.in); int N = scan.nextInt();
int x = 2; for(int i = 0; i <= N; i++)
{
int count = 0; for(int j = 1; j <= x; j++)
if(x%j == 0)
count++; if(count == 2)
System.out.print(x + " "); x++;
}
}

class driverFuction {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int x = 2;
int n = scanner.nextInt();

//count for n numbers
for(int i = 0; i <= n; i++) {
//reset count = 0;
int count = 0;

for (int j = 1; j <= x; j++) {
if (x % j == 0) count++;
}

if (count == 2) System.out.println("prime is x = " + x);

//add x
x++;
}
}
}


import java.util.Scanner;

public class ExArraySortElement
{
public static void main(String[] args)
{
int n, temp;
//scanner class object creation
Scanner s = new Scanner(System.in); //input total number of elements to be read
System.out.print("Enter the elements you want : ");
n = s.nextInt(); //integer array object
int a[] = new int[n]; //read elements
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
//数组中的元素需要一个个地输入
} //sorting elements
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
} //print sorted elements
System.out.println("Ascending Order:");
for (int i = 0; i < n ; i++)
{
System.out.println(a[i]);
}
}
}
// package whatever; // don't place package name!

import java.io.*;
import java.util.*;
import java.lang.*; class driverFuction {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(); //new an array
int[] nums = new int[n]; //input n numbers
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
} //sort
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (nums[i] > nums[j]) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
} //output
for (int i = 0; i < n; i++) {
System.out.println("nums[i] = " + nums[i]);
}
}
}

https://www.includehelp.com/java-programs/sort-an-array-in-ascending-order.aspx

import java.util.Scanner;
class PrimeCheck
{
public static void main(String args[])
{
int temp;
boolean isPrime=true;
Scanner scan= new Scanner(System.in);
System.out.println("Enter any number:");
//capture the input in an integer
int num=scan.nextInt();
scan.close();
for(int i=2;i<=num/2;i++)
{
temp=num%i;
if(temp==0)
{
isPrime=false;
break;
}
}
//If isPrime is true then the number is prime else not
if(isPrime)
System.out.println(num + " is a Prime Number");
else
System.out.println(num + " is not a Prime Number");
}
}

https://beginnersbook.com/2014/01/java-program-to-check-prime-number/


// package whatever; // don't place package name!

import java.io.*;
import java.util.*;
import java.lang.*;

class Palindrome
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);

System.out.println("Enter a string to check if it is a palindrome");
String original = in.nextLine();

//int length = original.length();

String clean = original.replaceAll("\\s+", "").toLowerCase();

//单词里面有空格,要用replaceall
int length = clean.length();
int forward = 0;
int backward = length - 1;
while (backward > forward) {
char forwardChar = clean.charAt(forward++);
char backwardChar = clean.charAt(backward--);
if (forwardChar != backwardChar)
System.out.println("false");
}
System.out.println("true");
}
}


import java.io.*;
import java.util.*;
import java.lang.*;


class Palindrome
{
public static void main(String args[])
{
String word, File file;
int count = 0;
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String nextToken = scanner.next();//表示下一行
if (nextToken.equalsIgnoreCase(word))
count++;
}
return count;
}
}



最新文章

  1. (转)深入浅出 妙用Javascript中apply、call、bind
  2. 『MySQL』索引类型 normal, unique, full text
  3. Effective C++ -----条款12: 复制对象时勿忘其每一个成分
  4. 将Log4net的配置配置到的独立文件中
  5. 每日一九度之 题目1038:Sum of Factorials
  6. Intellij IDEA,WebStorm-keymap(转)
  7. sails 相关文章
  8. Centos硬盘IO性能检测命令iostat[转]
  9. python之路--day11---迭代器和生成器
  10. 【LOJ#3096】[SNOI2019]数论
  11. 利用Python代码编写计算器小程序
  12. Vue2.0 新手完全填坑攻略—从环境搭建到发布
  13. web中静态资源和动态资源的概念及区别
  14. Hadoop 综合揭秘——HBase的原理与应用
  15. linux每日命令(2):cd命令
  16. 【刷题】BZOJ 2134 单选错位
  17. python自动制作gif并添加文字
  18. HTML5 Canvas ( 画一个五角星 ) lineJoin miterLimit
  19. Jmeter之Bean shell使用
  20. 【BZOJ】4720: [Noip2016]换教室

热门文章

  1. eclipse+PyDev遇到字符UTF-8的问题
  2. pyalgotrade入门
  3. Linux下定时切割Mongodb数据库日志并删除指定天数前的日志记录
  4. Spark和YARN
  5. Remi 安装源
  6. log4net 使用指南,最常遇到的问题整理。。。
  7. java.nio.charset.UnsupportedCharsetException: cp0
  8. Java利用ScriptEngineManager对计算公式的支持
  9. python编程遇见的异常
  10. java中如何制定自定义异常