java入门起步

最近跟着队友也学起了java 自己一边查书一边百度一边写点入门程序 一边写一边做笔记 完全是从零开始 大神勿喷

持续更新中————

1.输入输出小程序(a+b)

package java入门;//包名 java入门


import java.util.Scanner;//导入Scanner 有了这一句才能使用Scanner输入数据
public class java1 {//类名为java1
public static void main(String[] args){//主方法
Scanner in=new Scanner(System.in);//必须要有这个函数才能进行输入 这里的in决定了下面
int p,q;
p=in.nextInt();//输入函数,通过这个函数读取输入的整数,这里的in与Scanner函数对应
q=in.nextInt();
System.out.println(p);//print和printf都是输出数据就结束了,而println是输出一个数加换行
System.out.println(q);
System.out.println(p+q);//感觉java这里的加号有点麻烦
}


}

System.out.println()目前的发现

括号里输出字符串和字符后面可以加a(char a=‘\n’)来进行换行,但是数字后面加a不会换行 而是数字增大了10.

2.数组的创立及输出输出

一维数组

package java入门;//包名 java入门


import java.util.Scanner;//导入Scanner
public class java1 {//类名为java1
public static void main(String[] args){//主方法
Scanner in=new Scanner(System.in);
int p,q;
p=in.nextInt();//输入函数
int a[]=new int[p];//创立的时候与c++不同的地方必须要这样开数组
for(int i=0;i<p;i++)//输入输出与与c++相差无几
{
a[i]=in.nextInt();
}
for(int i=0;i<p;i++)
{
System.out.println(a[i]);
}
}


}

二位数组与一维数组的相似 int a[][]=new int [p][p]


3.java大数a+b

package java入门;
import java.util.Scanner;
import java.math.BigInteger;//多了一个相当于头文件的语句
public class java1 {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
BigInteger a=in.nextBigInteger();
BigInteger b=in.nextBigInteger();
BigInteger c=new BigInteger("0");
c=a.add(b);//这里用了加法函数
System.out.println(c);
}
}

拿着上面的代码去交hdu的第一道题错了 因为这个代码只能计算一次数据 没法持续输入然后持续计算 求教了队友老郭 才a掉

ac代码:

package java入门;
import java.util.Scanner;
import java.math.BigInteger;
public class Main {   //向oj交题时这个地方的包名只能是Main 别的包名都会ce
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        while(in.hasNext())   //神奇的代码 有了这个whil循环才能让程序持续的运行
        {
        BigInteger a=in.nextBigInteger();
        BigInteger b=in.nextBigInteger();
        BigInteger c=new BigInteger("0");
        c=a.add(b);
        System.out.println(c);
        }
    }
}

4.今天晚上去vju上做了几水题 加深了一下对java的理解

上题目-----

A - 大菲波数

   Fibonacci数列,定义如下:
f(1)=f(2)=1
f(n)=f(n-1)+f(n-2) n>=3。
计算第n项Fibonacci数值。
Input
输入第一行为一个整数N,接下来N行为整数Pi(1<=Pi<=1000)。
Output
输出为N行,每行为对应的f(Pi)。
Sample Input
5
1
2
3
4
5
Sample Output
1
1
2
3
5

ac代码:

             

B - A + B Problem II

(hdu的1002 水题)
   I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
import java.math.BigInteger;
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        BigInteger a, b;
        int T;
        Scanner in = new Scanner(System.in);
        T = in.nextInt();
        for (int i = 1; i <= T; ++i) {
            System.out.println("Case" + " " + i + ":");
            a = in.nextBigInteger();
            b = in.nextBigInteger();
            if (i < T) {//这个地方注意分类 最后一个case输出就不在输出空行了 而前面的都要多输出一个空行 以前在hdu上错了好几次都是因为这个
                System.out.println(a + " + " + b + " = " + a.add(b) );
                System.out.println();
            } else {
                System.out.println(a + " + " + b + " = " + a.add(b));
            }
        }
    }
} 

   

D - 大明A+B(大小数的a+b)

话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫“大明”。
这时他已经不是那个只会做100以内加法的那个“小明”了,现在他甚至会任意长度的正小数的加法。

现在,给你两个正的小数A和B,你的任务是代表大明计算出A+B的值。
Input
本题目包含多组测试数据,请处理到文件结束。
每一组测试数据在一行里面包含两个长度不大于400的正小数A和B。
Output
请在一行里面输出输出A+B的值,请输出最简形式。详细要求请见Sample Output。
Sample Input
1.1 2.9
1.1111111111 2.3444323343
1 1.1
Sample Output
4
3.4555434454
2.1
import java.math.BigDecimal;  //多了一个import语句 开大小数的 不过有简便方法 math.*可以代替所有的math.……
import java.math.BigInteger;  
import java.util.Scanner;  
public class Main {  
    public static void main(String[] args) {  
        Scanner input=new Scanner(System.in);  
        while(input.hasNext())  
        { 
            BigDecimal a=input.nextBigDecimal();  //定义和输出和大整数没啥区别
            BigDecimal b=input.nextBigDecimal();  
            BigDecimal c=a.add(b);  
            String str=c.stripTrailingZeros().toPlainString();  
  
            System.out.println(str);  
        }  
    }  
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值