Kết quả 1 đến 3 của 3
  1. #1
    Ngày tham gia
    Apr 2014
    Bài viết
    0

    Java cơ bản: không nhập được chuỗi hoTen lần 2.

    Mã nguồn PHP:
    [color=#000000]
    package homework[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/color][color=#007700];public class [/color][color=#0000bb]sinhvien [/color][color=#007700]{public [/color][color=#0000bb]string hoten[/color][color=#007700]=new [/color][color=#0000bb]string[/color][color=#007700]();public [/color][color=#0000bb]int age[/color][color=#007700],[/color][color=#0000bb]toan[/color][color=#007700],[/color][color=#0000bb]li[/color][color=#007700],[/color][color=#0000bb]hoa[/color][color=#007700];public static [/color][color=#0000bb]float tb[/color][color=#007700];private static [/color][color=#0000bb]scanner nhap[/color][color=#007700];public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]){[/color][color=#0000bb]sinhvien sv1[/color][color=#007700]=new [/color][color=#0000bb]sinhvien[/color][color=#007700]();[/color][color=#0000bb]sinhvien sv2[/color][color=#007700]=new [/color][color=#0000bb]sinhvien[/color][color=#007700]();[/color][color=#0000bb]nhap [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700]); [/color][color=#ff8000]//nhap du lieu cho sinh vien 1[/color][color=#0000bb]long start[/color][color=#007700]=[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]currenttimemillis[/color][color=#007700]();try{[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"nhap ten sv1:"[/color][color=#007700]);[/color][color=#0000bb]sv1[/color][color=#007700].[/color][color=#0000bb]hoten[/color][color=#007700]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]();[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"tuoi: "[/color][color=#007700]);[/color][color=#0000bb]sv1[/color][color=#007700].[/color][color=#0000bb]age[/color][color=#007700]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700]();[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"toan:"[/color][color=#007700]);[/color][color=#0000bb]sv1[/color][color=#007700].[/color][color=#0000bb]toan[/color][color=#007700]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700]();[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"li: "[/color][color=#007700]);[/color][color=#0000bb]sv1[/color][color=#007700].[/color][color=#0000bb]li[/color][color=#007700]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700]();[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"hoa: "[/color][color=#007700]);[/color][color=#0000bb]sv1[/color][color=#007700].[/color][color=#0000bb]hoa[/color][color=#007700]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700]();[/color][color=#0000bb]tb[/color][color=#007700]=(float) ([/color][color=#0000bb]sv1[/color][color=#007700].[/color][color=#0000bb]toan[/color][color=#007700]+[/color][color=#0000bb]sv1[/color][color=#007700].[/color][color=#0000bb]li[/color][color=#007700]+[/color][color=#0000bb]sv1[/color][color=#007700].[/color][color=#0000bb]hoa[/color][color=#007700])/[/color][color=#0000bb]3[/color][color=#007700]; [/color][color=#ff8000]//xuat du lieu sv1[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"-----"[/color][color=#007700]+[/color][color=#0000bb]sv1[/color][color=#007700].[/color][color=#0000bb]hoten[/color][color=#007700]+[/color][color=#dd0000]"
    "[/color][color=#007700]+[/color][color=#0000bb]sv1[/color][color=#007700].[/color][color=#0000bb]age[/color][color=#007700]+[/color][color=#dd0000]"
    "[/color][color=#007700]+[/color][color=#0000bb]sv1[/color][color=#007700].[/color][color=#0000bb]toan[/color][color=#007700]+[/color][color=#dd0000]"
    "[/color][color=#007700]+[/color][color=#0000bb]sv1[/color][color=#007700].[/color][color=#0000bb]li[/color][color=#007700]+[/color][color=#dd0000]"
    "[/color][color=#007700]+[/color][color=#0000bb]sv1[/color][color=#007700].[/color][color=#0000bb]hoa[/color][color=#007700]+[/color][color=#dd0000]"
    -----"[/color][color=#007700]);if([/color][color=#0000bb]tb[/color][color=#007700]>[/color][color=#0000bb]8[/color][color=#007700])[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"hoc sinh gioi"[/color][color=#007700]);else{[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"binh thuong"[/color][color=#007700]);} [/color][color=#ff8000]//nhap du lieu cho sinh vien 2[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"nhap ten sv2:"[/color][color=#007700]);[/color][color=#ff8000]//sv2.hoten=nhap.nextline();[/color][color=#0000bb]sv2[/color][color=#007700].[/color][color=#0000bb]hoten[/color][color=#007700]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]();[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"tuoi: "[/color][color=#007700]);[/color][color=#0000bb]sv2[/color][color=#007700].[/color][color=#0000bb]age[/color][color=#007700]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700]();[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"toan:"[/color][color=#007700]);[/color][color=#0000bb]sv2[/color][color=#007700].[/color][color=#0000bb]toan[/color][color=#007700]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700]();[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"li: "[/color][color=#007700]);[/color][color=#0000bb]sv2[/color][color=#007700].[/color][color=#0000bb]li[/color][color=#007700]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700]();[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"hoa: "[/color][color=#007700]);[/color][color=#0000bb]sv2[/color][color=#007700].[/color][color=#0000bb]hoa[/color][color=#007700]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700]();[/color][color=#0000bb]tb[/color][color=#007700]=(float) ([/color][color=#0000bb]sv2[/color][color=#007700].[/color][color=#0000bb]toan[/color][color=#007700]+[/color][color=#0000bb]sv2[/color][color=#007700].[/color][color=#0000bb]li[/color][color=#007700]+[/color][color=#0000bb]sv2[/color][color=#007700].[/color][color=#0000bb]hoa[/color][color=#007700])/[/color][color=#0000bb]3[/color][color=#007700]; [/color][color=#ff8000]//xuat du lieu sv2[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"-----"[/color][color=#007700]+[/color][color=#0000bb]sv2[/color][color=#007700].[/color][color=#0000bb]hoten[/color][color=#007700]+[/color][color=#dd0000]"
    "[/color][color=#007700]+[/color][color=#0000bb]sv2[/color][color=#007700].[/color][color=#0000bb]age[/color][color=#007700]+[/color][color=#dd0000]"
    "[/color][color=#007700]+[/color][color=#0000bb]sv2[/color][color=#007700].[/color][color=#0000bb]toan[/color][color=#007700]+[/color][color=#dd0000]"
    "[/color][color=#007700]+[/color][color=#0000bb]sv2[/color][color=#007700].[/color][color=#0000bb]li[/color][color=#007700]+[/color][color=#dd0000]"
    "[/color][color=#007700]+[/color][color=#0000bb]sv2[/color][color=#007700].[/color][color=#0000bb]hoa[/color][color=#007700]+[/color][color=#dd0000]"
    -----"[/color][color=#007700]);if([/color][color=#0000bb]tb[/color][color=#007700]>[/color][color=#0000bb]8[/color][color=#007700])[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"hoc sinh gioi"[/color][color=#007700]);else[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"binh thuong"[/color][color=#007700]);}catch([/color][color=#0000bb]exception e[/color][color=#007700]){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"nhap du lieu bi loi: error-"[/color][color=#007700]+[/color][color=#0000bb]e[/color][color=#007700]);}[/color][color=#0000bb]long end[/color][color=#007700]=[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]currenttimemillis[/color][color=#007700]();[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"thoi gian cau lenh: "[/color][color=#007700]+([/color][color=#0000bb]end[/color][color=#007700]-[/color][color=#0000bb]start[/color][color=#007700])); }}[/color] 

  2. #2
    Ngày tham gia
    Apr 2016
    Bài viết
    0
    1. sao bạn không dùng vòng lặp mà lại chạy 2 lần như thế
    2. bạn để ý lần nhập cuối cùng của sv1 dữ liệu ở kiểu int ( nhap.nextin(); )
    dữ liệu của họ tên sinh viên 2 ở kiểu string, từ giờ về sau nếu bạn đang nhập int mà chuyển sang nhập string ở 2 đối tượng khác nhau bạn thêm 1 dòng dauvao.nextline(); vào trước đầu vào của dữ liệu dạng string( trong bài này thêm dòng nhap.nextline(); ở trước dòng sv2.hoten=nhap.nextline(); là được

  3. #3
    Ngày tham gia
    Sep 2014
    Bài viết
    0
    của bạn bị trượt dòng rồi. bạn nên để 2 scanner 1 cái nhập số 1 cái nhập chữ. hoặc thêm nhap.nextline() vào giữa nhập số và nhập chữ.

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •