首页
统计
留言
Search
1
测试1
1,974 阅读
2
测试2
1,262 阅读
3
typecho内嵌B站视频
1,053 阅读
4
测试3神经猫
1,029 阅读
5
Matlab 计算机视觉实验-UI
978 阅读
Java学习记录
Matlab学习记录
测试
路径规划学习
登录
Search
Mango57
累计撰写
22
篇文章
累计收到
2
条评论
首页
栏目
Java学习记录
Matlab学习记录
测试
路径规划学习
页面
统计
留言
搜索到
22
篇与
mango57
的结果
2022-05-01
Java Test
简易记事本package Test; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetEncoder; public class Jishiben implements ActionListener { private Frame frame; private FileDialog fd_load; private FileDialog fd_save; private TextArea ta; private String file=""; private MenuItem save; private RandomAccessFile raf; private FileChannel fci; private FileLock flock; private CharsetEncoder encoder; private CharsetDecoder decoder; public static void main(String[] args) { new Jishiben().init(); } public void init(){ frame=new Frame("我的笔记本"); MenuBar mb=new MenuBar(); Menu file=new Menu("file"); Menu help=new Menu("help"); MenuItem open=new MenuItem("open"); save=new MenuItem("save"); save.setEnabled(false); file.add(open); file.add(save); mb.add(file); mb.add(help); frame.setMenuBar(mb); ta=new TextArea(); frame.add(ta,"Center"); open.addActionListener(this); save.addActionListener(this); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.setSize(600,400); frame.setLocation(300,100); frame.setVisible(true); fd_load=new FileDialog(frame,"打开文件",FileDialog.LOAD); fd_save=new FileDialog(frame,"保存文件",FileDialog.SAVE); Charset charset=Charset.forName(System.getProperty("file.encoding")); encoder= charset.newEncoder(); decoder= charset.newDecoder(); } public void actionPerformed(ActionEvent e){ String s=e.getActionCommand(); if (s.equals("open")){ fd_load.setVisible(true); String d=fd_load.getDirectory(); String f=fd_load.getFile(); if ((d!=null)&&(f!=null)){ String destfile=d+f; if (destfile.equals(file)){ return; }else { this.closeFile(); file=destfile; this.loadFile(); } } }else if (s.equals("save")){ this.saveFile(); } } private void saveFile() { String content= ta.getText(); try{ CharBuffer cb=CharBuffer.wrap(content.toCharArray()); ByteBuffer bb=encoder.encode(cb); raf.setLength(0); fci.write(bb); fci.force(true); }catch (IOException e){ e.printStackTrace(); } } private void loadFile() { try{ raf=new RandomAccessFile(file,"rw"); fci=raf.getChannel(); flock=fci.tryLock(); if (flock==null){ ta.setText(""); JOptionPane.showMessageDialog(null,"文件正在使用中喔","错误提示",JOptionPane.ERROR_MESSAGE); file=""; raf.close(); raf=null; }else { int length=(int)fci.size(); ByteBuffer bb=ByteBuffer.allocate(length); fci.read(bb); bb.flip(); CharBuffer cb=decoder.decode(bb); ta.setText(cb.toString()); frame.setTitle("我的笔记本"+file); save.setEnabled(true); } }catch(IOException e){ e.printStackTrace(); } } private void closeFile() { try{ if (flock!=null){ flock.release(); } if (raf!=null){ raf.close(); } file=""; frame.setTitle("我的记事本"); save.setEnabled(false); }catch(IOException e){ e.printStackTrace(); } } }
2022年05月01日
360 阅读
0 评论
0 点赞
2022-05-01
Matlab 计算机视觉实验-UI
UI代码classdef app1 < matlab.apps.AppBase % Properties that correspond to app components properties (Access = public) UIFigure matlab.ui.Figure UIAxes2 matlab.ui.control.UIAxes UIAxes matlab.ui.control.UIAxes Panel matlab.ui.container.Panel Button matlab.ui.control.Button Button_3 matlab.ui.control.Button Button_2 matlab.ui.control.Button SobelButton matlab.ui.control.Button CannyButton matlab.ui.control.Button Button_4 matlab.ui.control.Button Button_5 matlab.ui.control.Button Button_6 matlab.ui.control.Button Button_7 matlab.ui.control.Button Button_8 matlab.ui.control.Button Button_9 matlab.ui.control.Button Button_10 matlab.ui.control.Button Button_12 matlab.ui.control.Button Button_13 matlab.ui.control.Button end properties (Access = private) end % Callbacks that handle component events methods (Access = private) % Button pushed function: Button function ButtonPushed(app, event) global I flie K [filename,filepath] = uigetfile({ '*.jpg';'*.bmp';'*.tif';'*.png';'*.jpeg'},'载入图像'); if isequal(filename,0)||isequal(filepath,0) errordlg('没有选中文件','出错'); return; else %file=[pathname,filename]; file=[filepath,filename]; I=fullfile(file); K=imread(file); imshow(K,'Parent',app.UIAxes2); end end % Button pushed function: Button_2 function Button_2Pushed(app, event) global I BW K %[x,map]=imread(K); %BW = rgb2gray([x,map]); BW = rgb2gray(K); %app.Image2.ImageSource=I1; imshow(BW,'Parent',app.UIAxes); end % Button pushed function: SobelButton function SobelButtonPushed(app, event) global I K %[x,map]=imread(I); %BW = rgb2gray([x,map]); BW = rgb2gray(K); BW1=edge(BW,'Sobel'); imshow(BW1,'Parent',app.UIAxes); end % Button pushed function: CannyButton function CannyButtonPushed(app, event) global K %[x,map]=imread(I); %BW = rgb2gray([x,map]); BW = rgb2gray(K); BW2=edge(BW,'Canny'); imshow(BW2,'Parent',app.UIAxes); end % Button pushed function: Button_3 function Button_3Pushed(app, event) global K noise1 noise1= imnoise(K,'salt & pepper'); imshow(noise1,'Parent',app.UIAxes); end % Button pushed function: Button_4 function Button_4Pushed(app, event) global K noise2 noise2= imnoise(K,'gaussian'); imshow(noise2,'Parent',app.UIAxes); end % Button pushed function: Button_5 function Button_5Pushed(app, event) global K faceDetector = vision.CascadeObjectDetector(); % 构造检测器对象。 facebox = step(faceDetector, K); % 开始检测,将结果存储到facebox变量中 finalImage = insertShape(K, 'Rectangle', facebox,'LineWidth',5); imshow(finalImage,'Parent',app.UIAxes); end % Button pushed function: Button_6 function Button_6Pushed(app, event) global vid faceDetector = vision.CascadeObjectDetector(); vid = videoinput('winvideo', 1, 'YUY2_640x480'); set(vid,'ReturnedColorSpace','rgb'); vidRes=get(vid,'VideoResolution'); width=vidRes(1); height=vidRes(2); nBands=get(vid,'NumberOfBands'); figure('Name', 'Matlab调用摄像头', 'NumberTitle', 'Off', 'ToolBar', 'None', 'MenuBar', 'None'); uicontrol('String', 'Close', 'Callback', 'close(gcf)'); hImage=image(zeros(vidRes(2),vidRes(1),nBands)); preview(vid,hImage); end % Button pushed function: Button_7 function Button_7Pushed(app, event) faceDetector = vision.CascadeObjectDetector(); %enable viola jones algorithm bbox = [100 100 100 100]; vidDevice = imaq.VideoDevice('winvideo',1,'YUY2_640x480','ROI', [1 1 640 480],'ReturnedColorSpace','rgb'); boxInserter = vision.ShapeInserter('BorderColor','Custom','CustomBorderColor',[0 255 0]); nFrame =300; vidInfo = imaqhwinfo(vidDevice); vidHeight = vidInfo.MaxHeight; vidWidth = vidInfo.MaxWidth; videoPlayer = vision.VideoPlayer('Position',[300 100 640+30 480+30]); for k = 1:nFrame % start recording with 300 frames tic; % timer start videoFrame = step(vidDevice); % enable the image capture by webcam bbox = 4 * faceDetector.step(imresize(videoFrame, 1/4)); % boost video's fps videoOut = step(boxInserter, videoFrame, bbox); % highlight the boxes of face at video %release(boxInserter); step(videoPlayer, videoOut); % display the video live in video player end end % Button pushed function: Button_8 function Button_8Pushed(app, event) global I K %[x,map]=imread(I); %ori_im = rgb2gray([x,map]); %ori_im = imread(I); %ori_im = rgb2gray(uint8(ori_im)); ori_im = rgb2gray(uint8(K)); % fx = [5 0 -5;8 0 -8;5 0 -5]; % 高斯函数一阶微分,x方向(用于改进的Harris角点提取算法) fx = [-2 -1 0 1 2]; % x方向梯度算子(用于Harris角点提取算法) Ix = filter2(fx,ori_im); % x方向滤波 % fy = [5 8 5;0 0 0;-5 -8 -5]; % 高斯函数一阶微分,y方向(用于改进的Harris角点提取算法) fy = [-2;-1;0;1;2]; % y方向梯度算子(用于Harris角点提取算法) Iy = filter2(fy,ori_im); % y方向滤波 Ix2 = Ix.^2; Iy2 = Iy.^2; Ixy = Ix.*Iy; clear Ix; clear Iy; h= fspecial('gaussian',[7 7],2); % 产生7*7的高斯窗函数,sigma=2 Ix2 = filter2(h,Ix2); Iy2 = filter2(h,Iy2); Ixy = filter2(h,Ixy); height = size(ori_im,1); width = size(ori_im,2); result = zeros(height,width); % 纪录角点位置,角点处值为1 R = zeros(height,width); for i = 1:height for j = 1:width M = [Ix2(i,j) Ixy(i,j);Ixy(i,j) Iy2(i,j)]; % auto correlation matrix R(i,j) = det(M)-0.06*(trace(M))^2; end end cnt = 0; for i = 2:height-1 for j = 2:width-1 % 进行非极大抑制,窗口大小3*3 if R(i,j) > R(i-1,j-1) && R(i,j) > R(i-1,j) && R(i,j) > R(i-1,j+1) && R(i,j) > R(i,j-1) && R(i,j) > R(i,j+1) && R(i,j) > R(i+1,j-1) && R(i,j) > R(i+1,j) && R(i,j) > R(i+1,j+1) result(i,j) = 1; cnt = cnt+1; end end end Rsort=zeros(cnt,1); [posr, posc] = find(result == 1); for i=1:cnt Rsort(i)=R(posr(i),posc(i)); end [Rsort,ix]=sort(Rsort,1); Rsort=flipud(Rsort); ix=flipud(ix); ps=100; posr2=zeros(ps,1); posc2=zeros(ps,1); for i=1:ps posr2(i)=posr(ix(i)); posc2(i)=posc(ix(i)); end imshow(ori_im,'Parent',app.UIAxes); hold (app.UIAxes,'on') plot(app.UIAxes,posc2,posr2,'g+'); toc; end % Button pushed function: Button_9 function Button_9Pushed(app, event) cameraCalibrator; end % Button pushed function: Button_10 function Button_10Pushed(app, event) global I K %moveImg=imread(I); moveImg=rgb2gray(K); [imagePoints,boardSize] = detectCheckerboardPoints(moveImg);%检测棋盘格角点 %figure(1) %imshow(moveImg); %hold on; %plot(imagePoints(:,1),imagePoints(:,2),'ro'); movingPoints=imagePoints; %hold off % fixedImg=imread('4.jpg'); % fixedImg=rgb2gray(fixedImg); [imagePoints_fixedImg,boardSize_fixedImg] = detectCheckerboardPoints(fixedImg); %figure(2) %imshow(fixedImg); %hold on; %plot(imagePoints_fixedImg(:,1),imagePoints_fixedImg(:,2),'ro'); %hold off tempPoints1=reshape(imagePoints_fixedImg(:,1),boardSize_fixedImg(1)-1,boardSize_fixedImg(2)-1); tempPoints2=reshape(imagePoints_fixedImg(:,2),boardSize_fixedImg(1)-1,boardSize_fixedImg(2)-1); fixedPoints1=tempPoints1(1:boardSize(1)-1,1:boardSize(2)-1); fixedPoints2=tempPoints2(1:boardSize(1)-1,1:boardSize(2)-1); fixedPoints=[fixedPoints1(:),fixedPoints2(:)]; mytform = cp2tform(movingPoints,fixedPoints, 'projective');%求出投影变化的矩阵 registered = imtransform(moveImg, mytform, 'bicubic');%进行变化 imshow(imadjust(registered),'Parent',app.UIAxes); end % Button pushed function: Button_12 function Button_12Pushed(app, event) global I K %[x,map]=imread(I); %BW = rgb2gray([x,map]); BW = rgb2gray(K); W = fspecial('gaussian',[5,5],1); G = imfilter(BW, W, 'replicate'); imshow(G,'Parent',app.UIAxes); end % Button pushed function: Button_13 function Button_13Pushed(app, event) global K video_obj = videoinput('winvideo', 1, 'YUY2_640x480'); set(video_obj,'ReturnedColorSpace','rgb'); videoRes = get(video_obj, 'VideoResolution'); nBands = get(video_obj, 'NumberOfBands'); K = getsnapshot(video_obj); imshow(K,'parent',app.UIAxes2); imwrite(K,'C:\Users\17709\Desktop\gui\paizhao.jpg') end end % Component initialization methods (Access = private) % Create UIFigure and components function createComponents(app) % Create UIFigure and hide until all components are created app.UIFigure = uifigure('Visible', 'off'); app.UIFigure.Position = [100 100 855 563]; app.UIFigure.Name = 'UI Figure'; % Create UIAxes2 app.UIAxes2 = uiaxes(app.UIFigure); title(app.UIAxes2, '原图') xlabel(app.UIAxes2, '') ylabel(app.UIAxes2, '') app.UIAxes2.FontUnits = 'points'; app.UIAxes2.FontSize = 9; app.UIAxes2.CLim = [0 1]; app.UIAxes2.XColor = 'none'; app.UIAxes2.YColor = 'none'; app.UIAxes2.TitleFontWeight = 'bold'; app.UIAxes2.Position = [31 294 377 270]; % Create UIAxes app.UIAxes = uiaxes(app.UIFigure); title(app.UIAxes, '处理后的图') xlabel(app.UIAxes, '') ylabel(app.UIAxes, '') app.UIAxes.AmbientLightColor = 'none'; app.UIAxes.MinorGridLineStyle = 'none'; app.UIAxes.GridColor = 'none'; app.UIAxes.MinorGridColor = 'none'; app.UIAxes.XColor = 'none'; app.UIAxes.YColor = 'none'; app.UIAxes.TitleFontWeight = 'bold'; app.UIAxes.BackgroundColor = [0.9412 0.9412 0.9412]; app.UIAxes.Position = [472 294 371 270]; % Create Panel app.Panel = uipanel(app.UIFigure); app.Panel.ForegroundColor = [1 0 0]; app.Panel.TitlePosition = 'centertop'; app.Panel.Title = '功能区'; app.Panel.BackgroundColor = [0.9412 0.9412 0.9412]; app.Panel.FontSize = 18; app.Panel.Position = [31 33 796 238]; % Create Button app.Button = uibutton(app.Panel, 'push'); app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true); app.Button.Position = [670 12 113 47]; app.Button.Text = '打开图片'; % Create Button_3 app.Button_3 = uibutton(app.Panel, 'push'); app.Button_3.ButtonPushedFcn = createCallbackFcn(app, @Button_3Pushed, true); app.Button_3.Position = [26 149 100 25]; app.Button_3.Text = '椒盐噪声'; % Create Button_2 app.Button_2 = uibutton(app.Panel, 'push'); app.Button_2.ButtonPushedFcn = createCallbackFcn(app, @Button_2Pushed, true); app.Button_2.Position = [195 149 100 25]; app.Button_2.Text = '灰度化'; % Create SobelButton app.SobelButton = uibutton(app.Panel, 'push'); app.SobelButton.ButtonPushedFcn = createCallbackFcn(app, @SobelButtonPushed, true); app.SobelButton.Position = [195 107 100 22]; app.SobelButton.Text = 'Sobel'; % Create CannyButton app.CannyButton = uibutton(app.Panel, 'push'); app.CannyButton.ButtonPushedFcn = createCallbackFcn(app, @CannyButtonPushed, true); app.CannyButton.Position = [195 58 100 22]; app.CannyButton.Text = 'Canny'; % Create Button_4 app.Button_4 = uibutton(app.Panel, 'push'); app.Button_4.ButtonPushedFcn = createCallbackFcn(app, @Button_4Pushed, true); app.Button_4.Position = [26 106 100 25]; app.Button_4.Text = '高斯噪声'; % Create Button_5 app.Button_5 = uibutton(app.Panel, 'push'); app.Button_5.ButtonPushedFcn = createCallbackFcn(app, @Button_5Pushed, true); app.Button_5.Position = [348 106 100 25]; app.Button_5.Text = '人脸检测'; % Create Button_6 app.Button_6 = uibutton(app.Panel, 'push'); app.Button_6.ButtonPushedFcn = createCallbackFcn(app, @Button_6Pushed, true); app.Button_6.Position = [499 105 100 26]; app.Button_6.Text = '摄像头预览'; % Create Button_7 app.Button_7 = uibutton(app.Panel, 'push'); app.Button_7.ButtonPushedFcn = createCallbackFcn(app, @Button_7Pushed, true); app.Button_7.Position = [499 57 100 25]; app.Button_7.Text = '摄像头人脸检测'; % Create Button_8 app.Button_8 = uibutton(app.Panel, 'push'); app.Button_8.ButtonPushedFcn = createCallbackFcn(app, @Button_8Pushed, true); app.Button_8.Position = [348 149 100 25]; app.Button_8.Text = '角点检测'; % Create Button_9 app.Button_9 = uibutton(app.Panel, 'push'); app.Button_9.ButtonPushedFcn = createCallbackFcn(app, @Button_9Pushed, true); app.Button_9.Position = [642 149 100 25]; app.Button_9.Text = '相机标定'; % Create Button_10 app.Button_10 = uibutton(app.Panel, 'push'); app.Button_10.ButtonPushedFcn = createCallbackFcn(app, @Button_10Pushed, true); app.Button_10.Position = [642 105 100 26]; app.Button_10.Text = '倾斜矫正'; % Create Button_12 app.Button_12 = uibutton(app.Panel, 'push'); app.Button_12.ButtonPushedFcn = createCallbackFcn(app, @Button_12Pushed, true); app.Button_12.Position = [26 56 100 26]; app.Button_12.Text = '高斯平滑滤波'; % Create Button_13 app.Button_13 = uibutton(app.Panel, 'push'); app.Button_13.ButtonPushedFcn = createCallbackFcn(app, @Button_13Pushed, true); app.Button_13.Position = [499 148 100 26]; app.Button_13.Text = '拍照'; % Show the figure after all components are created app.UIFigure.Visible = 'on'; end end % App creation and deletion methods (Access = public) % Construct app function app = app1 % Create UIFigure and components createComponents(app) % Register the app with App Designer registerApp(app, app.UIFigure) if nargout == 0 clear app end end % Code that executes before app deletion function delete(app) % Delete UIFigure when app is deleted delete(app.UIFigure) end end end
2022年05月01日
978 阅读
0 评论
0 点赞
2022-04-30
Java初学Method
01package method; public class Test1 { //main方法 public static void main(String[] args) { //实际参数:实际调用传递给他的参数 int sum = add(1, 2); System.out.println(sum); //test(); } //加法 //形式参数,用来定义作用的 public static int add(int a,int b){ return a+b; } public static void test(){ for (int i = 0; i <= 1000; i++) { if (i%5==0){ System.out.print(i+"\t"); } if (i%(5*3)==0){ System.out.println(); } } } }02
2022年04月30日
322 阅读
0 评论
0 点赞
2022-04-30
Java初学Struct
Breakpackage struct; public class BreakTest1 { public static void main(String[] args) { int i=0; while (i<100){ i++; System.out.println(i); if (i==30){ break; } } System.out.println("123"); }; }Continuepackage struct; public class ContinueTest1 { public static void main(String[] args) { int i=0; while (i<100){ i++; if (i%10==0){ System.out.println(); continue; } System.out.print(i); } /* Break在任何循环语句的主体部分,均可用break控制循环的流程。 break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用) continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。 */ } }DoWhile 01package struct; public class DoWhileTest1 { public static void main(String[] args) { int i=0; int sum=0; do { sum=sum+i; i++; }while (i<=100); System.out.println(sum); System.out.println(i); } }DoWhile 02package struct; public class DoWhileTest2 { public static void main(String[] args) { int a=0; while (a<0){ System.out.println(a); a++; } System.out.println("============================="); do { System.out.println(a); a++; }while (a<0); System.out.println("a的值:"+a); } }For 01package struct; public class ForTest1 { public static void main(String[] args) { int a=1;//初始化条件 while (a<=100){//条件判断 System.out.println(a);//循环体 a+=2;//迭代 a+=2→a=a+2 } System.out.println("while循环结束!"); //初始化值//条件判断//迭代 for (int i=1;i<=100;i++){ System.out.println(i); } System.out.println("for循环结束!"); /* 关于For循环有以下几点说明: 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。 然后,检测布尔表达式的值,如果为true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。 执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。 再次检测布尔表达式。循环执行上面的过程。 */ //死循环 for (; ; ) { } } }For 02package struct; public class ForTest2 { public static void main(String[] args) { //练习1:计算0到100之间的奇数和偶数的和 int oddsum=0; int evensum=0; int a=0,b=0; for (int i = 0; i <=100; i++) { if(i%2!=0){//奇数 oddsum+=i;//oddsum=oddsum+i a=i; }else{//偶数 evensum+=i; b=i; } } System.out.println("奇数的和:"+oddsum); System.out.println("偶数的和:"+evensum); System.out.println(a); System.out.println(b); } }For 03package struct; public class ForTest3 { public static void main(String[] args) { //练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个 for (int i = 0; i <= 1000; i++) { if(i%5==0){ System.out.print(i+"\t"); } if (i%(5*3)==0){ System.out.println(); //System.out.print("\n"); } } //println 输出完会换行 //print 输出完不会换行 } }For 04package struct; public class ForTest4 { public static void main(String[] args) { //练习3:打印九九乘法表 /* */ for (int j = 1; j <= 9; j++) { for (int i = 1; i <= j; i++) { System.out.print(j+"*"+i+"="+(j*i)+"\t"); } System.out.println(); } } }For 05package struct; public class ForTest5 { public static void main(String[] args) { int [] numbers={10,20,30,40,50};//定义数组 for (int i = 0; i < 5; i++) { System.out.println(numbers[i]); } System.out.println("================"); //遍历数组的元素 for (int x:numbers){ System.out.println(x); } } }If 01package struct; import java.awt.image.SampleModel; import java.util.Scanner; public class IfTest1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入内容:"); String s = scanner.nextLine(); //equals:判断字符串是否相等 if(s.equals("Hello")){ System.out.println(s); } System.out.println("End"); scanner.close(); } }If 02package struct; import java.util.Scanner; public class IfTest2 { public static void main(String[] args) { //考试分数大于60就是及格,小于60分就是不及格。 Scanner scanner=new Scanner(System.in); System.out.println("请输入成绩:"); int score = scanner.nextInt(); if (score>=60){ System.out.println("及格"); }else{ System.out.println("不及格"); } scanner.close(); } }If 03package struct; import java.util.Scanner; public class IfTest3 { public static void main(String[] args) { //考试分数大于60分就是及格,小于60分就不及格 Scanner scanner=new Scanner(System.in); /** * if语句至多有一个else语句,else语句在所有的else if语句之后 * if语句可以有若干个else if语句,他们必须在else语句之前 * 一旦其中一个else if语句检测为true,其他的else if以及else语句都将跳过执行。 */ System.out.println("请输入成绩:"); int score=scanner.nextInt(); if (score==100){ System.out.println("恭喜满分"); }else if (score<100&&score>=90){ System.out.println("A级"); }else if (score<90&&score>=80){ System.out.println("B级"); }else if (score<80&&score>=70){ System.out.println("C级"); }else if (score<70&&score>=60){ System.out.println("D级"); }else if (score<60&&score>=0){ System.out.println("不及格"); }else { System.out.println("成绩不合法"); } scanner.close(); } }Labelpackage struct; public class LabelTest { public static void main(String[] args) { //打印101-105之间所有的质数(只有能被1和它本身能整除的数) int count=0; outer:for(int i=101;i<150;i++){ for (int j=2;j<i/2;j++){ if (i%j==0){ continue outer; } } System.out.print(i+" "); } } }Shunxupackage struct; public class ShunXuTest1 { public static void main(String[] args) { System.out.println("Hello1"); System.out.println("Hello2"); System.out.println("Hello3"); System.out.println("Hello4"); System.out.println("Hello5"); } }Switch 01package struct; import jdk.swing.interop.SwingInterOpUtils; import java.util.Scanner; public class SwitchTest1 { public static void main(String[] args) { //case穿透 没有break会往下执行后面的case //switch 匹配一个具体的值 char grade='C'; switch (grade){ case 'A': System.out.println("优秀"); break;//可选 case 'B': System.out.println("良好"); break;//可选 case 'C': System.out.println("及格"); break;//可选 case 'D': System.out.println("再接再厉"); break;//可选 case 'E': System.out.println("挂科"); break;//可选 default: System.out.println("未知等级"); } } }Switch 02package struct; public class SwitchTest2 { public static void main(String[] args) { String name="mango2"; //JDK7的新特性,表达式结果可以是字符串!!! //字符串的本质还是数字 //反编译 java---class(字节码文件)----反编译(IDEA) switch(name){ case "mango1": System.out.println("mango1"); break; case "mango2": System.out.println("mango2"); break; default: System.out.println("都不是"); } } }While 01package struct; public class WhileTest1 { public static void main(String[] args) { //输出1~100 int i=0; while (i<100){ i++; System.out.println(i); } } }While 02package struct; public class WhileTest2 { public static void main(String[] args) { //死循环 while (true){ //等待客户端连接 //定时检查 //。。。。 } } }While 03package struct; public class WhileTest3 { public static void main(String[] args) { //计算1+2+3+...+100=? int i=0; int sum=0; while (i<=100){ sum=sum+i; i++; } System.out.println(sum); System.out.println(i); } }Test1 (打印三角形)package struct; public class Test1 { public static void main(String[] args) { //打印三角形 5行 for (int i = 1; i <= 5; i++) { for (int j = 5; j >=i ; j--) { System.out.print(" "); } for (int j = 1; j <=i ; j++) { System.out.print("*"); } for (int j = 1; j <i ; j++) { System.out.print("*"); } System.out.println(); } } }
2022年04月30日
370 阅读
0 评论
0 点赞
2022-04-30
Java初学Scanner
01package scanner; import java.util.Scanner; public class Test1 { public static void main(String[] args) { //创建一个扫描对象,用于接收键盘数据 Scanner scanner = new Scanner(System.in); System.out.println("使用next方式接受:"); //判断用户有没有输入字符串 if(scanner.hasNext()){ String str=scanner.next();//有空格就断了 以空格为结束符 System.out.println("输入的内容为:"+str); } //凡是属于IO流的类,如果不关闭会一直占用资源,要养成好习惯,用完就关掉 scanner.close(); } }02package scanner; import java.util.Scanner; public class Test2 { public static void main(String[] args) { //从键盘接收数据 Scanner scanner = new Scanner(System.in); System.out.println("使用nextLine方式接受:"); //判断是否还有输入 if(scanner.hasNextLine()){ String str = scanner.nextLine();//有空格也没关系,以回车为结束符 System.out.println("输入的内容为:"+str); } scanner.close(); } }03package scanner; import java.util.Scanner; public class Test3 { public static void main(String[] args) { //从键盘接收数据 Scanner scanner = new Scanner(System.in); System.out.println("请输入数据:"); String str = scanner.nextLine(); System.out.println("输入的内容为:"+str); scanner.close(); } }04package scanner; import java.util.Scanner; public class Test4 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); //从键盘接收数据 int i=0; float f=0.0f; System.out.println("请输入整数:"); //如果。。。那么。。。 if (scanner.hasNextInt()){ i=scanner.nextInt(); System.out.println("整数数据:"+i); } else{ System.out.println("输入的不是整数数据:"+i); } System.out.println("请输入小数:"); //如果。。。那么。。。 if (scanner.hasNextFloat()){ f=scanner.nextFloat(); System.out.println("小数数据:"+f); } else{ System.out.println("输入的不是小数数据:"+f); } scanner.close(); } }05package scanner; import java.util.Scanner; public class Test5 { public static void main(String[] args) { //我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果 Scanner scanner = new Scanner(System.in); //和 double sum=0; //计算输入了多少个数字 int m=0; System.out.println("请输入数据:"); //通过循环判断是否还有输入,并在里面对每一次进行求和和统计 while (scanner.hasNextDouble()) { double x = scanner.nextDouble(); m=m+1;//m++ sum=sum+x; System.out.println("你输入了第"+m+"个输入,然后当前结果sum="+sum); } System.out.println(m+"个数的和为"+sum); System.out.println(m+"个数的平均值是"+(sum/m)); scanner.close(); } }
2022年04月30日
344 阅读
0 评论
0 点赞
1
2
3
4
5